MCPcopy Create free account
hub / github.com/astercloud/aster / parseStreamEvent

Method parseStreamEvent

pkg/provider/glm.go:417–506  ·  view source on GitHub ↗

parseStreamEvent 解析流式事件

(event map[string]any)

Source from the content-addressed store, hash-verified

415
416// parseStreamEvent 解析流式事件
417func (gp *GLMProvider) parseStreamEvent(event map[string]any) *StreamChunk {
418 ctx := context.Background() // 用于日志记录
419
420 // GLM API 使用 OpenAI 兼容格式
421 chunk := &StreamChunk{}
422
423 // 检查 choices
424 if choices, ok := event["choices"].([]any); ok && len(choices) > 0 {
425 if choice, ok := choices[0].(map[string]any); ok {
426 if delta, ok := choice["delta"].(map[string]any); ok {
427 // 检查是否有 tool_calls(OpenAI 格式)
428 if toolCalls, ok := delta["tool_calls"].([]any); ok && len(toolCalls) > 0 {
429 // 工具调用开始
430 if toolCall, ok := toolCalls[0].(map[string]any); ok {
431 index := 0
432 if idx, ok := toolCall["index"].(float64); ok {
433 index = int(idx)
434 }
435
436 chunk.Type = "content_block_start"
437 chunk.Index = index
438
439 // 构建工具调用信息(转换为 Anthropic 格式以便统一处理)
440 toolInfo := map[string]any{
441 "type": "tool_use",
442 }
443
444 if id, ok := toolCall["id"].(string); ok {
445 toolInfo["id"] = id
446 }
447
448 if fn, ok := toolCall["function"].(map[string]any); ok {
449 if name, ok := fn["name"].(string); ok {
450 toolInfo["name"] = name
451 }
452 // arguments 会在 content_block_delta 中逐步接收
453 }
454
455 chunk.Delta = toolInfo
456 glmLog.Debug(ctx, "received tool_use block", map[string]any{"index": index, "id": toolInfo["id"], "name": toolInfo["name"]})
457 return chunk
458 }
459 }
460
461 // 检查是否有文本内容
462 if content, ok := delta["content"].(string); ok && content != "" {
463 chunk.Type = "content_block_delta"
464 chunk.Delta = map[string]any{
465 "type": "text_delta",
466 "text": content,
467 }
468 return chunk
469 }
470 }
471
472 // 检查 tool_calls 的增量更新(arguments 字段)
473 if delta, ok := choice["delta"].(map[string]any); ok {
474 if toolCalls, ok := delta["tool_calls"].([]any); ok && len(toolCalls) > 0 {

Callers 1

processStreamMethod · 0.95

Calls 1

DebugMethod · 0.65

Tested by

no test coverage detected