MCPcopy Index your code
hub / github.com/Louisym/MiniCC / PortRuntime

Class PortRuntime

src/runtime.py:89–192  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

87
88
89class PortRuntime:
90 def route_prompt(self, prompt: str, limit: int = 5) -> list[RoutedMatch]:
91 tokens = {token.lower() for token in prompt.replace('/', ' ').replace('-', ' ').split() if token}
92 by_kind = {
93 'command': self._collect_matches(tokens, PORTED_COMMANDS, 'command'),
94 'tool': self._collect_matches(tokens, PORTED_TOOLS, 'tool'),
95 }
96
97 selected: list[RoutedMatch] = []
98 for kind in ('command', 'tool'):
99 if by_kind[kind]:
100 selected.append(by_kind[kind].pop(0))
101
102 leftovers = sorted(
103 [match for matches in by_kind.values() for match in matches],
104 key=lambda item: (-item.score, item.kind, item.name),
105 )
106 selected.extend(leftovers[: max(0, limit - len(selected))])
107 return selected[:limit]
108
109 def bootstrap_session(self, prompt: str, limit: int = 5) -> RuntimeSession:
110 context = build_port_context()
111 setup_report = run_setup(trusted=True)
112 setup = setup_report.setup
113 history = HistoryLog()
114 engine = QueryEnginePort.from_workspace()
115 history.add('context', f'python_files={context.python_file_count}, archive_available={context.archive_available}')
116 history.add('registry', f'commands={len(PORTED_COMMANDS)}, tools={len(PORTED_TOOLS)}')
117 matches = self.route_prompt(prompt, limit=limit)
118 registry = build_execution_registry()
119 command_execs = tuple(registry.command(match.name).execute(prompt) for match in matches if match.kind == 'command' and registry.command(match.name))
120 tool_execs = tuple(registry.tool(match.name).execute(prompt) for match in matches if match.kind == 'tool' and registry.tool(match.name))
121 denials = tuple(self._infer_permission_denials(matches))
122 stream_events = tuple(engine.stream_submit_message(
123 prompt,
124 matched_commands=tuple(match.name for match in matches if match.kind == 'command'),
125 matched_tools=tuple(match.name for match in matches if match.kind == 'tool'),
126 denied_tools=denials,
127 ))
128 turn_result = engine.submit_message(
129 prompt,
130 matched_commands=tuple(match.name for match in matches if match.kind == 'command'),
131 matched_tools=tuple(match.name for match in matches if match.kind == 'tool'),
132 denied_tools=denials,
133 )
134 persisted_session_path = engine.persist_session()
135 history.add('routing', f'matches={len(matches)} for prompt={prompt!r}')
136 history.add('execution', f'command_execs={len(command_execs)} tool_execs={len(tool_execs)}')
137 history.add('turn', f'commands={len(turn_result.matched_commands)} tools={len(turn_result.matched_tools)} denials={len(turn_result.permission_denials)} stop={turn_result.stop_reason}')
138 history.add('session_store', persisted_session_path)
139 return RuntimeSession(
140 prompt=prompt,
141 context=context,
142 setup=setup,
143 setup_report=setup_report,
144 system_init_message=build_system_init_message(trusted=True),
145 history=history,
146 routed_matches=matches,

Callers 4

routeMethod · 0.85
mainFunction · 0.85

Calls

no outgoing calls