Inject per-request tool credentials into lazyllm globals. tool_config maps tool names to credential tokens or token lists:: { "feishu": "u-xxx", # OAuth2 access token (caller is responsible for freshness) "bing": "sk-xxx", "google": ["AIza...",
(tool_config: Optional[Dict[str, Any]])
| 44 | |
| 45 | |
| 46 | def inject_tool_config(tool_config: Optional[Dict[str, Any]]) -> None: |
| 47 | '''Inject per-request tool credentials into lazyllm globals. |
| 48 | |
| 49 | tool_config maps tool names to credential tokens or token lists:: |
| 50 | |
| 51 | { |
| 52 | "feishu": "u-xxx", # OAuth2 access token (caller is responsible for freshness) |
| 53 | "bing": "sk-xxx", |
| 54 | "google": ["AIza...", "AIza..."], |
| 55 | } |
| 56 | |
| 57 | The destination config key for each tool is determined by |
| 58 | :data:`TOOL_AUTH_REGISTRY`. Unknown tools fall back to |
| 59 | ``dynamic_tool_auth``. |
| 60 | |
| 61 | After this call, globals.config is updated, e.g.:: |
| 62 | |
| 63 | globals.config['dynamic_fs_auth'] = {..., 'feishu': 'u-xxx'} |
| 64 | globals.config['dynamic_tool_auth'] = {..., 'bing': 'sk-xxx', 'google': 'AIza...'} |
| 65 | ''' |
| 66 | if not tool_config: |
| 67 | return |
| 68 | |
| 69 | # Collect updates grouped by config key. |
| 70 | updates: Dict[str, Dict[str, Any]] = {} |
| 71 | injected: list = [] |
| 72 | |
| 73 | for tool_name, token in tool_config.items(): |
| 74 | if isinstance(token, str): |
| 75 | value = token.strip() |
| 76 | elif isinstance(token, (list, tuple)) and all(isinstance(k, str) for k in token): |
| 77 | keys = [k.strip() for k in token if k.strip()] |
| 78 | value = keys if len(keys) > 1 else (keys[0] if keys else '') |
| 79 | else: |
| 80 | LOG.warning(f'[inject_tool_config] skipping {tool_name!r}: expected str or list[str] token, ' |
| 81 | f'got {type(token).__name__}') |
| 82 | continue |
| 83 | if not value: |
| 84 | LOG.warning(f'[inject_tool_config] skipping {tool_name!r}: token is empty') |
| 85 | continue |
| 86 | |
| 87 | canonical = tool_name.lower().strip() |
| 88 | config_key = TOOL_AUTH_REGISTRY.get(canonical, _DEFAULT_CONFIG_KEY) |
| 89 | |
| 90 | updates.setdefault(config_key, {})[canonical] = value |
| 91 | injected.append(canonical) |
| 92 | |
| 93 | for config_key, new_entries in updates.items(): |
| 94 | existing = lazyllm.globals.config[config_key] or {} |
| 95 | lazyllm.globals.config[config_key] = {**existing, **new_entries} |
| 96 | |
| 97 | LOG.info(f'[inject_tool_config] injected tools: {sorted(injected)}') |