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

Method parseStreamChunk

pkg/provider/gemini.go:431–503  ·  view source on GitHub ↗

parseStreamChunk 解析单个流式 chunk

(chunk map[string]any)

Source from the content-addressed store, hash-verified

429
430// parseStreamChunk 解析单个流式 chunk
431func (p *GeminiProvider) parseStreamChunk(chunk map[string]any) []StreamChunk {
432 result := make([]StreamChunk, 0)
433
434 // 获取 candidates
435 candidates, ok := chunk["candidates"].([]any)
436 if !ok || len(candidates) == 0 {
437 return result
438 }
439
440 candidate := candidates[0].(map[string]any)
441
442 // 获取 content
443 content, ok := candidate["content"].(map[string]any)
444 if !ok {
445 // 检查是否完成
446 if finishReason, ok := candidate["finishReason"].(string); ok {
447 result = append(result, StreamChunk{
448 Type: string(ChunkTypeDone),
449 FinishReason: strings.ToLower(finishReason),
450 })
451 }
452 return result
453 }
454
455 // 获取 parts
456 parts, ok := content["parts"].([]any)
457 if !ok {
458 return result
459 }
460
461 // 解析每个 part
462 for _, partData := range parts {
463 part := partData.(map[string]any)
464
465 // 文本内容
466 if text, ok := part["text"].(string); ok && text != "" {
467 result = append(result, StreamChunk{
468 Type: string(ChunkTypeText),
469 TextDelta: text,
470 Delta: text,
471 })
472 }
473
474 // 函数调用
475 if functionCall, ok := part["functionCall"].(map[string]any); ok {
476 name := functionCall["name"].(string)
477 args := functionCall["args"].(map[string]any)
478
479 // 序列化参数
480 argsJSON, _ := json.Marshal(args)
481
482 result = append(result, StreamChunk{
483 Type: string(ChunkTypeToolCall),
484 ToolCall: &ToolCallDelta{
485 Index: 0,
486 Name: name,
487 ArgumentsDelta: string(argsJSON),
488 },

Callers 1

parseSSEStreamMethod · 0.95

Calls 1

parseUsageFromMapMethod · 0.95

Tested by

no test coverage detected