MCPcopy Create free account
hub / github.com/YuJunZhiXue/qwen2API / parseJSONStringForSchema

Function parseJSONStringForSchema

backend/toolcall/parser.go:370–399  ·  view source on GitHub ↗
(value string, wantArray, wantObject bool)

Source from the content-addressed store, hash-verified

368}
369
370func parseJSONStringForSchema(value string, wantArray, wantObject bool) (any, bool) {
371 stripped := strings.TrimSpace(value)
372 if stripped == "" {
373 return value, false
374 }
375 candidates := []string{stripped}
376 if wantArray && !strings.HasPrefix(stripped, "[") {
377 candidates = append(candidates, "["+stripped+"]")
378 }
379 for _, candidate := range candidates {
380 var parsed any
381 if json.Unmarshal([]byte(candidate), &parsed) != nil {
382 continue
383 }
384 if wantArray {
385 if _, ok := parsed.([]any); ok {
386 return parsed, true
387 }
388 if m, ok := parsed.(map[string]any); ok {
389 return []any{m}, true
390 }
391 }
392 if wantObject {
393 if _, ok := parsed.(map[string]any); ok {
394 return parsed, true
395 }
396 }
397 }
398 return value, false
399}
400
401func toolSchema(name string, tools []map[string]any) map[string]any {
402 for _, tool := range tools {

Callers 1

coerceValueBySchemaFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected