@agentscope-ai/agentscope 0.0.5 → 0.0.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.
Files changed (44) hide show
  1. package/dist/agent/index.d.mts +4 -4
  2. package/dist/agent/index.d.ts +4 -4
  3. package/dist/agent/index.js +34 -7
  4. package/dist/agent/index.js.map +1 -1
  5. package/dist/agent/index.mjs +34 -7
  6. package/dist/agent/index.mjs.map +1 -1
  7. package/dist/{base-qmU135_k.d.ts → base-13VLaOvY.d.ts} +2 -2
  8. package/dist/{base-BI5s2ksj.d.mts → base-Bc3GkNS7.d.mts} +1 -1
  9. package/dist/{base-DHtZCg94.d.ts → base-CJkm56kB.d.ts} +1 -1
  10. package/dist/{base-CFDeoJRe.d.ts → base-Dfizi3RB.d.ts} +1 -1
  11. package/dist/{base-BDyDUIhj.d.mts → base-L72wZVx8.d.mts} +2 -2
  12. package/dist/{base-BB9eTlit.d.mts → base-Ps8E0j1_.d.mts} +1 -1
  13. package/dist/event/index.d.mts +2 -2
  14. package/dist/event/index.d.ts +2 -2
  15. package/dist/event/index.js.map +1 -1
  16. package/dist/event/index.mjs.map +1 -1
  17. package/dist/formatter/index.d.mts +2 -2
  18. package/dist/formatter/index.d.ts +2 -2
  19. package/dist/formatter/index.js +16 -0
  20. package/dist/formatter/index.js.map +1 -1
  21. package/dist/formatter/index.mjs +16 -0
  22. package/dist/formatter/index.mjs.map +1 -1
  23. package/dist/message/index.d.mts +1 -1
  24. package/dist/message/index.d.ts +1 -1
  25. package/dist/message/index.js +50 -14
  26. package/dist/message/index.js.map +1 -1
  27. package/dist/message/index.mjs +50 -14
  28. package/dist/message/index.mjs.map +1 -1
  29. package/dist/{message-D-LObC06.d.mts → message-COpNEf0G.d.mts} +10 -7
  30. package/dist/{message-DU0_qm3u.d.ts → message-DbCMy5tM.d.ts} +10 -7
  31. package/dist/model/index.d.mts +4 -4
  32. package/dist/model/index.d.ts +4 -4
  33. package/dist/model/index.js +16 -0
  34. package/dist/model/index.js.map +1 -1
  35. package/dist/model/index.mjs +16 -0
  36. package/dist/model/index.mjs.map +1 -1
  37. package/dist/storage/index.d.mts +2 -2
  38. package/dist/storage/index.d.ts +2 -2
  39. package/package.json +1 -1
  40. package/src/agent/agent.ts +20 -7
  41. package/src/event/index.ts +2 -2
  42. package/src/message/append-event.test.ts +17 -11
  43. package/src/message/message.test.ts +2 -2
  44. package/src/message/message.ts +77 -14
@@ -1,5 +1,5 @@
1
- import { S as StorageBase, A as AgentState } from '../base-BB9eTlit.mjs';
2
- import { M as Msg } from '../message-D-LObC06.mjs';
1
+ import { S as StorageBase, A as AgentState } from '../base-Ps8E0j1_.mjs';
2
+ import { M as Msg } from '../message-COpNEf0G.mjs';
3
3
  import '../index-CAxQAkiP.mjs';
4
4
  import '../block-BqWf-Qcb.mjs';
5
5
  import '../event/index.mjs';
@@ -1,5 +1,5 @@
1
- import { S as StorageBase, A as AgentState } from '../base-DHtZCg94.js';
2
- import { M as Msg } from '../message-DU0_qm3u.js';
1
+ import { S as StorageBase, A as AgentState } from '../base-CJkm56kB.js';
2
+ import { M as Msg } from '../message-DbCMy5tM.js';
3
3
  import '../index-CAxQAkiP.js';
4
4
  import '../block-BqWf-Qcb.js';
5
5
  import '../event/index.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentscope-ai/agentscope",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "",
5
5
  "exports": {
6
6
  "./message": {
@@ -261,26 +261,39 @@ export class Agent {
261
261
  * @param usage
262
262
  */
263
263
  protected _saveToContext(blocks: ContentBlock[], usage?: ChatUsage): void {
264
+ const msgUsage: Msg['usage'] = usage
265
+ ? { input_tokens: usage.inputTokens, output_tokens: usage.outputTokens }
266
+ : undefined;
264
267
  const lastMsg = this.context.at(-1);
265
268
  if (this.context.length === 0) {
266
269
  this.context.push(
267
- createMsg({ name: this.name, content: blocks, role: 'assistant', usage })
270
+ createMsg({
271
+ name: this.name,
272
+ content: blocks,
273
+ role: 'assistant',
274
+ usage: msgUsage,
275
+ })
268
276
  );
269
277
  } else if (lastMsg && lastMsg.role === 'assistant' && lastMsg.name === this.name) {
270
278
  lastMsg.content.push(...blocks);
271
- if (usage) {
279
+ if (msgUsage) {
272
280
  if (!lastMsg.usage) {
273
281
  lastMsg.usage = {
274
- inputTokens: 0,
275
- outputTokens: 0,
282
+ input_tokens: 0,
283
+ output_tokens: 0,
276
284
  };
277
285
  }
278
- lastMsg.usage.inputTokens = lastMsg.usage.inputTokens + usage.inputTokens;
279
- lastMsg.usage.outputTokens = lastMsg.usage.outputTokens + usage.outputTokens;
286
+ lastMsg.usage.input_tokens = lastMsg.usage.input_tokens + msgUsage.input_tokens;
287
+ lastMsg.usage.output_tokens = lastMsg.usage.output_tokens + msgUsage.output_tokens;
280
288
  }
281
289
  } else {
282
290
  this.context.push(
283
- createMsg({ name: this.name, content: blocks, role: 'assistant', usage })
291
+ createMsg({
292
+ name: this.name,
293
+ content: blocks,
294
+ role: 'assistant',
295
+ usage: msgUsage,
296
+ })
284
297
  );
285
298
  }
286
299
  }
@@ -160,7 +160,6 @@ export interface ToolResultTextDeltaEvent extends EventBase {
160
160
  type: EventType.TOOL_RESULT_TEXT_DELTA;
161
161
  reply_id: string;
162
162
  tool_call_id: string;
163
- block_id: string;
164
163
  delta: string;
165
164
  }
166
165
 
@@ -168,7 +167,8 @@ export interface ToolResultDataDeltaEvent extends EventBase {
168
167
  type: EventType.TOOL_RESULT_DATA_DELTA;
169
168
  reply_id: string;
170
169
  tool_call_id: string;
171
- block_id: string;
170
+ /** Auto-generated in {@link appendEvent} when not provided. */
171
+ block_id?: string;
172
172
  media_type: string;
173
173
  data?: string;
174
174
  url?: string;
@@ -8,6 +8,7 @@ import {
8
8
  ToolCallBlock,
9
9
  ToolResultBlock,
10
10
  } from './block';
11
+ import { PermissionRule } from '../permission';
11
12
 
12
13
  // Fixed IDs used throughout
13
14
  const REPLY_ID = 'reply_001';
@@ -84,15 +85,19 @@ function dbUrl(blockId: string, url: string, mediaType: string): DataBlock {
84
85
  * @param name
85
86
  * @param inp
86
87
  * @param state
88
+ * @param suggestedRules
87
89
  * @returns A tool call block object.
88
90
  */
89
91
  function tcb(
90
92
  tcId: string,
91
93
  name: string,
92
94
  inp: string,
93
- state: ToolCallBlock['state']
95
+ state: ToolCallBlock['state'],
96
+ suggestedRules?: PermissionRule[]
94
97
  ): ToolCallBlock {
95
- return { type: 'tool_call', id: tcId, name, input: inp, state };
98
+ const block: ToolCallBlock = { type: 'tool_call', id: tcId, name, input: inp, state };
99
+ if (suggestedRules !== undefined) block.suggested_rules = suggestedRules;
100
+ return block;
96
101
  }
97
102
 
98
103
  /**
@@ -370,7 +375,9 @@ describe('appendEvent', () => {
370
375
  reply_id: REPLY_ID,
371
376
  tool_calls: [tcAllowBlock],
372
377
  });
373
- groundTruths.push(base([...s4Prefix, tcb(TC_ALLOW, 'search', '{"q": "hi"}', 'asking')]));
378
+ groundTruths.push(
379
+ base([...s4Prefix, tcb(TC_ALLOW, 'search', '{"q": "hi"}', 'asking', [])])
380
+ );
374
381
 
375
382
  // UserConfirmResultEvent (confirmed=true) → state: asking → allowed
376
383
  events.push({
@@ -380,7 +387,9 @@ describe('appendEvent', () => {
380
387
  reply_id: REPLY_ID,
381
388
  confirm_results: [{ confirmed: true, tool_call: tcAllowBlock }],
382
389
  });
383
- groundTruths.push(base([...s4Prefix, tcb(TC_ALLOW, 'search', '{"q": "hi"}', 'allowed')]));
390
+ groundTruths.push(
391
+ base([...s4Prefix, tcb(TC_ALLOW, 'search', '{"q": "hi"}', 'allowed', [])])
392
+ );
384
393
 
385
394
  // ToolResult for TC_ALLOW - text output
386
395
  events.push({
@@ -391,7 +400,7 @@ describe('appendEvent', () => {
391
400
  tool_call_id: TC_ALLOW,
392
401
  tool_call_name: 'search',
393
402
  });
394
- const s4bPrefix = [...s4Prefix, tcb(TC_ALLOW, 'search', '{"q": "hi"}', 'allowed')];
403
+ const s4bPrefix = [...s4Prefix, tcb(TC_ALLOW, 'search', '{"q": "hi"}', 'allowed', [])];
395
404
  groundTruths.push(base([...s4bPrefix, trb(TC_ALLOW, 'search', [], 'running')]));
396
405
 
397
406
  // ToolResult text deltas
@@ -401,7 +410,6 @@ describe('appendEvent', () => {
401
410
  type: EventType.TOOL_RESULT_TEXT_DELTA,
402
411
  reply_id: REPLY_ID,
403
412
  tool_call_id: TC_ALLOW,
404
- block_id: 'auto_1',
405
413
  delta: 'Found:',
406
414
  });
407
415
  groundTruths.push(
@@ -422,7 +430,6 @@ describe('appendEvent', () => {
422
430
  type: EventType.TOOL_RESULT_TEXT_DELTA,
423
431
  reply_id: REPLY_ID,
424
432
  tool_call_id: TC_ALLOW,
425
- block_id: 'auto_1',
426
433
  delta: ' 3 items',
427
434
  });
428
435
  groundTruths.push(
@@ -501,7 +508,7 @@ describe('appendEvent', () => {
501
508
  reply_id: REPLY_ID,
502
509
  tool_calls: [tcDenyBlock],
503
510
  });
504
- groundTruths.push(base([...s5Prefix, tcb(TC_DENY, 'delete', '', 'asking')]));
511
+ groundTruths.push(base([...s5Prefix, tcb(TC_DENY, 'delete', '', 'asking', [])]));
505
512
 
506
513
  events.push({
507
514
  id: '26',
@@ -510,10 +517,10 @@ describe('appendEvent', () => {
510
517
  reply_id: REPLY_ID,
511
518
  confirm_results: [{ confirmed: false, tool_call: tcDenyBlock }],
512
519
  });
513
- groundTruths.push(base([...s5Prefix, tcb(TC_DENY, 'delete', '', 'finished')]));
520
+ groundTruths.push(base([...s5Prefix, tcb(TC_DENY, 'delete', '', 'finished', [])]));
514
521
 
515
522
  // Stage 6: ToolCall (TC_EXT) → external execution
516
- const s6Prefix = [...s5Prefix, tcb(TC_DENY, 'delete', '', 'finished')];
523
+ const s6Prefix = [...s5Prefix, tcb(TC_DENY, 'delete', '', 'finished', [])];
517
524
 
518
525
  events.push({
519
526
  id: '27',
@@ -753,7 +760,6 @@ describe('appendEvent', () => {
753
760
  type: EventType.TOOL_RESULT_TEXT_DELTA,
754
761
  reply_id: REPLY_ID,
755
762
  tool_call_id: 'ghost',
756
- block_id: 'b',
757
763
  delta: 'x',
758
764
  },
759
765
  {
@@ -27,8 +27,8 @@ describe('Message', () => {
27
27
 
28
28
  test('obtain different content from message', () => {
29
29
  const msg = createMsg({
30
- name: 'user',
31
- role: 'user',
30
+ name: 'assistant',
31
+ role: 'assistant',
32
32
  content: [
33
33
  { id: crypto.randomUUID(), type: 'text', text: 'Hello' },
34
34
  { id: crypto.randomUUID(), type: 'thinking', thinking: '...' },
@@ -29,8 +29,8 @@ export interface Msg {
29
29
  finished_at?: string | null;
30
30
  /** Usage information for the message, such as token counts. */
31
31
  usage?: {
32
- inputTokens: number;
33
- outputTokens: number;
32
+ input_tokens: number;
33
+ output_tokens: number;
34
34
  };
35
35
  }
36
36
 
@@ -48,6 +48,44 @@ export interface Msg {
48
48
  * @param root0.usage
49
49
  * @returns A Msg object.
50
50
  */
51
+ /**
52
+ * Validate that content blocks are allowed for the given role.
53
+ *
54
+ * Mirrors the Python `_assert_user_content_blocks` / `_assert_system_content_blocks`
55
+ * guards: user messages may only contain text or data blocks; system messages
56
+ * may only contain text blocks; assistant messages accept any block type.
57
+ * @param role
58
+ * @param content
59
+ */
60
+ function assertContentBlocksForRole(role: Msg['role'], content: ContentBlock[]): void {
61
+ if (role === 'user') {
62
+ for (const block of content) {
63
+ if (block.type !== 'text' && block.type !== 'data') {
64
+ throw new Error('User message can only contain text blocks or data blocks.');
65
+ }
66
+ }
67
+ } else if (role === 'system') {
68
+ for (const block of content) {
69
+ if (block.type !== 'text') {
70
+ throw new Error('System message can only contain text blocks.');
71
+ }
72
+ }
73
+ }
74
+ }
75
+
76
+ /**
77
+ * createMsg is a low-level utility for constructing Msg objects with proper defaults and validation.
78
+ * @param root0
79
+ * @param root0.name
80
+ * @param root0.content
81
+ * @param root0.role
82
+ * @param root0.metadata
83
+ * @param root0.id
84
+ * @param root0.created_at
85
+ * @param root0.finished_at
86
+ * @param root0.usage
87
+ * @returns A Msg object with the specified properties, and defaults for any omitted fields.
88
+ */
51
89
  export function createMsg({
52
90
  name,
53
91
  content,
@@ -65,6 +103,7 @@ export function createMsg({
65
103
  typeof content === 'string'
66
104
  ? [{ id: crypto.randomUUID(), type: 'text', text: content } as TextBlock]
67
105
  : content;
106
+ assertContentBlocksForRole(role, contentBlocks);
68
107
  return { id, name, role, content: contentBlocks, metadata, created_at, finished_at, usage };
69
108
  }
70
109
 
@@ -76,6 +115,7 @@ export function createMsg({
76
115
  * @param root0.metadata
77
116
  * @param root0.id
78
117
  * @param root0.created_at
118
+ * @param root0.finished_at
79
119
  * @returns A Msg object with role 'user'.
80
120
  */
81
121
  export function UserMsg({
@@ -84,14 +124,24 @@ export function UserMsg({
84
124
  metadata = {},
85
125
  id = crypto.randomUUID(),
86
126
  created_at = new Date().toISOString(),
127
+ finished_at,
87
128
  }: {
88
129
  name: string;
89
130
  content: string | ContentBlock[];
90
131
  metadata?: Record<string, JSONSerializableObject>;
91
132
  id?: string;
92
133
  created_at?: string;
134
+ finished_at?: string | null;
93
135
  }): Msg {
94
- return createMsg({ name, content, role: 'user', metadata, id, created_at });
136
+ return createMsg({
137
+ name,
138
+ content,
139
+ role: 'user',
140
+ metadata,
141
+ id,
142
+ created_at,
143
+ finished_at: finished_at ?? created_at,
144
+ });
95
145
  }
96
146
 
97
147
  /**
@@ -131,6 +181,7 @@ export function AssistantMsg({
131
181
  * @param root0.metadata
132
182
  * @param root0.id
133
183
  * @param root0.created_at
184
+ * @param root0.finished_at
134
185
  * @returns A Msg object with role 'system'.
135
186
  */
136
187
  export function SystemMsg({
@@ -139,14 +190,24 @@ export function SystemMsg({
139
190
  metadata = {},
140
191
  id = crypto.randomUUID(),
141
192
  created_at = new Date().toISOString(),
193
+ finished_at,
142
194
  }: {
143
195
  name: string;
144
196
  content: string | ContentBlock[];
145
197
  metadata?: Record<string, JSONSerializableObject>;
146
198
  id?: string;
147
199
  created_at?: string;
200
+ finished_at?: string | null;
148
201
  }): Msg {
149
- return createMsg({ name, content, role: 'system', metadata, id, created_at });
202
+ return createMsg({
203
+ name,
204
+ content,
205
+ role: 'system',
206
+ metadata,
207
+ id,
208
+ created_at,
209
+ finished_at: finished_at ?? created_at,
210
+ });
150
211
  }
151
212
 
152
213
  /**
@@ -267,7 +328,7 @@ export function appendEvent(msg: Msg, event: AgentEvent): Msg {
267
328
  const block = findBlock(msg, 'data', event.block_id);
268
329
  if (!block) {
269
330
  console.warn(`DataBlock "${event.block_id}" not found, skipping.`);
270
- } else {
331
+ } else if (event.data) {
271
332
  ((block as DataBlock).source as Base64Source).data += event.data;
272
333
  }
273
334
  break;
@@ -322,7 +383,7 @@ export function appendEvent(msg: Msg, event: AgentEvent): Msg {
322
383
  if (!last || last.type !== 'text') {
323
384
  trb.output.push({
324
385
  type: 'text',
325
- id: event.block_id ?? crypto.randomUUID(),
386
+ id: crypto.randomUUID(),
326
387
  text: event.delta,
327
388
  });
328
389
  } else {
@@ -345,7 +406,11 @@ export function appendEvent(msg: Msg, event: AgentEvent): Msg {
345
406
  event.data != null
346
407
  ? { type: 'base64', data: event.data, media_type: event.media_type }
347
408
  : { type: 'url', url: event.url!, media_type: event.media_type };
348
- trb.output.push({ type: 'data', id: event.block_id, source });
409
+ trb.output.push({
410
+ type: 'data',
411
+ id: event.block_id ?? crypto.randomUUID(),
412
+ source,
413
+ });
349
414
  }
350
415
  break;
351
416
  }
@@ -363,12 +428,12 @@ export function appendEvent(msg: Msg, event: AgentEvent): Msg {
363
428
  case EventType.MODEL_CALL_END:
364
429
  // Accumulated the input and output tokens here.
365
430
  if (msg.usage) {
366
- msg.usage.inputTokens += event.input_tokens;
367
- msg.usage.outputTokens += event.output_tokens;
431
+ msg.usage.input_tokens += event.input_tokens;
432
+ msg.usage.output_tokens += event.output_tokens;
368
433
  } else {
369
434
  msg.usage = {
370
- inputTokens: event.input_tokens,
371
- outputTokens: event.output_tokens,
435
+ input_tokens: event.input_tokens,
436
+ output_tokens: event.output_tokens,
372
437
  };
373
438
  }
374
439
  break;
@@ -378,9 +443,7 @@ export function appendEvent(msg: Msg, event: AgentEvent): Msg {
378
443
  const b = findBlock(msg, 'tool_call', tc.id);
379
444
  if (b) {
380
445
  (b as ToolCallBlock).state = 'asking';
381
- if (tc.suggested_rules !== undefined) {
382
- (b as ToolCallBlock).suggested_rules = tc.suggested_rules;
383
- }
446
+ (b as ToolCallBlock).suggested_rules = tc.suggested_rules || [];
384
447
  }
385
448
  }
386
449
  break;