Repository navigation
Expand file tree
/
Copy pathtools.py
More file actions
735 lines (685 loc) · 27.5 KB
/
Copy pathtools.py
File metadata and controls
735 lines (685 loc) · 27.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
import asyncio
import json
import time
from dataclasses import dataclass
from typing import Any, Awaitable, Callable, Dict, List, Optional
import httpx
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.agent.mcp import demo_mcp
from app.agent.memory import persist_confirmed_memory
from app.agent.validators import evaluate_tool_result
from app.core.config import settings
from app.models.entities import AgentRun, AgentStep, Document, ProgramSource, Task, ToolCall
from app.services.business import (
create_material_plan,
create_shortlist,
create_timeline,
get_or_create_profile,
get_program,
get_requirement,
search_programs_for_profile,
)
from app.services.program_discovery import discover_official_programs
from app.services.requirements import (
extract_and_save_requirements,
fetch_official_source,
verify_program_official,
)
ToolHandler = Callable[[AsyncSession, Dict[str, Any]], Awaitable[Any]]
@dataclass
class ToolSpec:
name: str
description: str
parameters: Dict[str, Any]
handler: ToolHandler
mutates_data: bool = False
approval_required: bool = False
timeout_seconds: Optional[int] = None
max_retries: Optional[int] = None
def as_openai_tool(self) -> Dict[str, Any]:
return {
"type": "function",
"name": self.name,
"description": self.description,
"parameters": self.parameters,
"strict": True,
}
def as_chat_tool(self) -> Dict[str, Any]:
return {
"type": "function",
"function": {
"name": self.name,
"description": self.description,
"parameters": self.parameters,
},
}
def object_schema(properties: Dict[str, Any], required: List[str]) -> Dict[str, Any]:
return {
"type": "object",
"properties": properties,
"required": required,
"additionalProperties": False,
}
def validate_arguments(schema: Dict[str, Any], arguments: Dict[str, Any]) -> None:
if not isinstance(arguments, dict):
raise ValueError("工具参数必须是对象")
properties = schema.get("properties", {})
missing = [name for name in schema.get("required", []) if name not in arguments]
if missing:
raise ValueError(f"缺少必填工具参数:{', '.join(missing)}")
if schema.get("additionalProperties") is False:
unknown = sorted(set(arguments) - set(properties))
if unknown:
raise ValueError(f"包含未声明工具参数:{', '.join(unknown)}")
python_types = {"string": str, "array": list, "object": dict, "number": (int, float)}
for name, value in arguments.items():
expected = properties.get(name, {}).get("type")
if expected in python_types and not isinstance(value, python_types[expected]):
raise ValueError(f"工具参数 {name} 类型应为 {expected}")
async def tool_get_profile(session: AsyncSession, arguments: Dict[str, Any]) -> Any:
item = await get_or_create_profile(session)
return {
"full_name": item.full_name,
"school": item.current_school,
"major": item.current_major,
"gpa": item.gpa,
"gpa_scale": item.gpa_scale,
"targets": item.target_fields,
"countries": item.target_countries,
"confirmed": item.confirmed,
}
async def tool_save_profile(session: AsyncSession, arguments: Dict[str, Any]) -> Any:
item = await get_or_create_profile(session)
for key, value in arguments.items():
setattr(item, key, value)
item.confirmed = True
await session.commit()
await session.refresh(item)
await persist_confirmed_memory(
session,
key="applicant_profile",
value={**arguments, "confirmed": True},
source_type="user_confirmed",
source_id=item.id,
)
return {"profile_id": item.id, "confirmed": item.confirmed, "status": "saved"}
async def tool_search_programs(session: AsyncSession, arguments: Dict[str, Any]) -> Any:
items = await search_programs_for_profile(
session,
query=arguments.get("query", ""),
country=arguments.get("country", ""),
field=arguments.get("field", ""),
)
return [
{
"id": item.id,
"university": item.university,
"name": item.name,
"degree": item.degree,
"country": item.country,
"field": item.field,
"tuition": item.tuition,
"official_url": item.official_url,
"warning": "未附官网逐字证据的招生字段均为待确认",
}
for item in items[:20]
]
async def tool_discover_official_programs(session: AsyncSession, arguments: Dict[str, Any]) -> Any:
items, catalog_report = await discover_official_programs(
session,
countries=arguments["countries"],
fields=arguments["fields"],
degree_level=arguments["degree_level"],
target_university=arguments["target_university"],
max_qs_rank=int(arguments["max_qs_rank"]) or None,
excluded_program_ids=set(arguments["excluded_program_ids"]),
max_candidates=int(arguments["max_candidates"]),
school_offset=int(arguments.get("school_offset") or 0),
include_report=True,
)
return {
"items": [
{
"id": item.id,
"university": item.university,
"name": item.name,
"degree": item.degree,
"country": item.country,
"field": item.field,
"qs_rank": item.qs_rank,
"official_url": item.official_url,
"catalog_url": item.catalog_url,
"faculty_catalog_url": item.faculty_catalog_url,
}
for item in items
],
"catalog_report": catalog_report,
}
async def tool_compare_programs(session: AsyncSession, arguments: Dict[str, Any]) -> Any:
output = []
for program_id in arguments["program_ids"]:
program = await get_program(session, program_id)
if not program:
continue
requirement = await get_requirement(session, program_id)
output.append(
{
"id": program.id,
"university": program.university,
"program": program.name,
"tuition": program.tuition,
"deadline": requirement.deadline if requirement else None,
"min_gpa": requirement.min_gpa if requirement else None,
"language": requirement.language if requirement else {},
"verified": bool(requirement and requirement.verified),
"official_url": program.official_url,
}
)
return output
async def tool_create_shortlist(session: AsyncSession, arguments: Dict[str, Any]) -> Any:
item = await create_shortlist(session, arguments["name"], arguments["program_ids"])
return {"shortlist_id": item.id, "name": item.name, "status": "created"}
async def tool_material_plan(session: AsyncSession, arguments: Dict[str, Any]) -> Any:
item = await create_material_plan(session, arguments["program_id"])
return {
"material_plan_id": item.id,
"checklist": item.checklist,
"cv_plan": item.cv_plan,
"ps_plan": item.ps_plan,
"gaps": item.gaps,
}
async def tool_timeline(session: AsyncSession, arguments: Dict[str, Any]) -> Any:
items = await create_timeline(session, arguments["program_id"])
return [{"id": item.id, "title": item.title, "due_date": item.due_date} for item in items]
async def tool_mcp_search(session: AsyncSession, arguments: Dict[str, Any]) -> Any:
return await demo_mcp.call_tool(session, "catalog.search_programs", arguments)
async def tool_parse_document(session: AsyncSession, arguments: Dict[str, Any]) -> Any:
item = await session.get(Document, arguments["document_id"])
if not item or item.owner_id != settings.local_owner_id:
raise ValueError("材料不存在")
return {
"document_id": item.id,
"filename": item.filename,
"kind": item.kind,
"parse_status": item.parse_status,
"candidate_data": item.extracted_data,
"requires_confirmation": True,
}
async def tool_fetch_official_source(session: AsyncSession, arguments: Dict[str, Any]) -> Any:
program = await get_program(session, arguments["program_id"])
if not program:
raise ValueError("项目不存在")
source = await fetch_official_source(session, program)
await session.commit()
return {
"program_id": program.id,
"source_id": source.id,
"url": source.url,
"title": source.title,
"content_hash": source.content_hash,
"status": source.status,
}
async def tool_extract_requirements(session: AsyncSession, arguments: Dict[str, Any]) -> Any:
program = await get_program(session, arguments["program_id"])
source = await session.get(ProgramSource, arguments["source_id"])
if not program or not source or source.program_id != program.id:
raise ValueError("项目或官网来源不存在")
result = await extract_and_save_requirements(session, program, source)
return {
"program_id": program.id,
"source_id": source.id,
"status": source.status,
**result,
}
async def tool_verify_program_official(session: AsyncSession, arguments: Dict[str, Any]) -> Any:
program = await get_program(session, arguments["program_id"])
if not program:
raise ValueError("项目不存在")
primary, extracted = await verify_program_official(
session, program, fast_mode=bool(arguments.get("fast_mode"))
)
evidence = list(extracted.get("evidence", []))
if arguments.get("fast_mode"):
evidence.insert(
0,
{
"field": "program_identity",
"quote": primary.title or program.name,
"value": program.name,
"confidence": 1.0,
"source_id": primary.id,
"url": primary.url,
},
)
return {
"program_id": program.id,
"primary_source_id": primary.id,
"status": "verified" if arguments.get("fast_mode") else primary.status,
"verification_scope": (
"official_program_identity" if arguments.get("fast_mode") else "admission_requirements"
),
"official_url": program.official_url,
"deadline": extracted.get("deadline"),
"deadlines": extracted.get("deadlines", []),
"tuition": extracted.get("tuition"),
"currency": extracted.get("currency", ""),
"materials": extracted.get("materials", []),
"language": extracted.get("language", {}),
"fees": extracted.get("fees", {}),
"evidence": evidence,
"source_results": extracted.get("source_results", []),
}
async def tool_update_task(session: AsyncSession, arguments: Dict[str, Any]) -> Any:
item = await session.get(Task, arguments["task_id"])
if not item or item.owner_id != settings.local_owner_id:
raise ValueError("任务不存在")
for key in ("status", "due_date", "priority", "details"):
if key in arguments:
setattr(item, key, arguments[key])
await session.commit()
await session.refresh(item)
return {"task_id": item.id, "status": item.status, "due_date": item.due_date}
class ToolRegistry:
def __init__(self) -> None:
self.tools: Dict[str, ToolSpec] = {}
self._register_defaults()
def register(self, tool: ToolSpec) -> None:
self.tools[tool.name] = tool
def _register_defaults(self) -> None:
self.register(
ToolSpec(
"get_applicant_profile",
"Read the confirmed applicant profile. Never infer missing facts.",
object_schema({}, []),
tool_get_profile,
)
)
self.register(
ToolSpec(
"save_applicant_profile",
"Save only applicant profile fields the user explicitly confirmed in this message.",
object_schema(
{
"full_name": {"type": "string"},
"current_school": {"type": "string"},
"current_major": {"type": "string"},
"degree": {"type": "string"},
"gpa": {"type": "number"},
"gpa_scale": {"type": "number"},
"language_scores": {"type": "object"},
"target_countries": {"type": "array", "items": {"type": "string"}},
"target_fields": {"type": "array", "items": {"type": "string"}},
"intake": {"type": "string"},
"budget": {"type": "number"},
"preferences": {"type": "object"},
},
[
"full_name",
"current_school",
"current_major",
"degree",
"gpa",
"gpa_scale",
"language_scores",
"target_countries",
"target_fields",
"intake",
"budget",
"preferences",
],
),
tool_save_profile,
mutates_data=True,
approval_required=True,
)
)
self.register(
ToolSpec(
"search_programs",
"Search the seeded program catalog. Returned facts may require official verification.",
object_schema(
{
"query": {"type": "string"},
"country": {"type": "string"},
"field": {"type": "string"},
},
["query", "country", "field"],
),
tool_search_programs,
)
)
self.register(
ToolSpec(
"discover_official_programs",
"Discover official university/faculty program catalogs first, enumerate catalog-bound program pages, validate identity and hard constraints, and cache current metadata.",
object_schema(
{
"countries": {"type": "array", "items": {"type": "string"}},
"fields": {"type": "array", "items": {"type": "string"}},
"degree_level": {"type": "string"},
"target_university": {"type": "string"},
"max_qs_rank": {"type": "number"},
"excluded_program_ids": {"type": "array", "items": {"type": "string"}},
"max_candidates": {"type": "number"},
"school_offset": {"type": "number"},
},
[
"countries",
"fields",
"degree_level",
"target_university",
"max_qs_rank",
"excluded_program_ids",
"max_candidates",
],
),
tool_discover_official_programs,
timeout_seconds=settings.program_discovery_operation_timeout_seconds,
max_retries=0,
)
)
self.register(
ToolSpec(
"compare_programs",
"Compare program requirements and official evidence status.",
object_schema(
{"program_ids": {"type": "array", "items": {"type": "string"}}},
["program_ids"],
),
tool_compare_programs,
)
)
self.register(
ToolSpec(
"build_shortlist",
"Create a shortlist in the YYGlobal workspace.",
object_schema(
{
"name": {"type": "string"},
"program_ids": {"type": "array", "items": {"type": "string"}},
},
["name", "program_ids"],
),
tool_create_shortlist,
mutates_data=True,
approval_required=True,
)
)
self.register(
ToolSpec(
"build_material_plan",
"Create grounded CV and PS material plans for one program.",
object_schema({"program_id": {"type": "string"}}, ["program_id"]),
tool_material_plan,
mutates_data=True,
approval_required=True,
)
)
self.register(
ToolSpec(
"build_application_timeline",
"Create application tasks from one program deadline.",
object_schema({"program_id": {"type": "string"}}, ["program_id"]),
tool_timeline,
mutates_data=True,
approval_required=True,
)
)
self.register(
ToolSpec(
"mcp_catalog_search",
"Read-only MCP adapter that searches the local program catalog.",
object_schema({"query": {"type": "string"}}, ["query"]),
tool_mcp_search,
)
)
self.register(
ToolSpec(
"parse_applicant_document",
"Read already parsed CV, PS, transcript or image candidates. Candidates are never confirmed facts.",
object_schema({"document_id": {"type": "string"}}, ["document_id"]),
tool_parse_document,
)
)
self.register(
ToolSpec(
"fetch_official_source",
"Fetch the exact official page configured for a university program and save a traceable source.",
object_schema({"program_id": {"type": "string"}}, ["program_id"]),
tool_fetch_official_source,
)
)
self.register(
ToolSpec(
"extract_program_requirements",
"Extract deadlines, tuition, materials and requirements from a fetched official source with verbatim evidence.",
object_schema(
{"program_id": {"type": "string"}, "source_id": {"type": "string"}},
["program_id", "source_id"],
),
tool_extract_requirements,
mutates_data=True,
)
)
self.register(
ToolSpec(
"verify_program_official",
"Research the program page and relevant same-program official admissions/requirements/tuition pages, then persist evidence-backed fields.",
object_schema(
{
"program_id": {"type": "string"},
"fast_mode": {"type": "boolean"},
},
["program_id"],
),
tool_verify_program_official,
mutates_data=True,
timeout_seconds=settings.program_discovery_timeout_seconds,
)
)
self.register(
ToolSpec(
"update_task",
"Update one application task after explicit user confirmation.",
object_schema(
{
"task_id": {"type": "string"},
"status": {"type": "string"},
"due_date": {"type": "string"},
"priority": {"type": "string"},
"details": {"type": "string"},
},
["task_id"],
),
tool_update_task,
mutates_data=True,
approval_required=True,
)
)
def allowed(self, names: List[str]) -> List[ToolSpec]:
return [self.tools[name] for name in names if name in self.tools]
async def execute(
self,
session: AsyncSession,
run_id: str,
name: str,
arguments: Dict[str, Any],
allowed_names: List[str],
approved: bool = False,
goal_spec: Optional[Dict[str, Any]] = None,
) -> Any:
if name not in allowed_names or name not in self.tools:
raise PermissionError(f"当前 Skill 无权使用工具:{name}")
tool = self.tools[name]
if tool.approval_required and not approved:
raise PermissionError(f"工具 {name} 需要用户明确确认")
validate_arguments(tool.parameters, arguments)
step = await self._resolve_step(session, run_id, name, goal_spec or {})
if goal_spec and goal_spec.get("recipe") == "program_research" and not step:
raise PermissionError(f"工具 {name} 不属于 Program Research Recipe 的任何步骤")
unmet_dependencies = await self._unmet_dependencies(session, run_id, step) if step else []
started = time.perf_counter()
trace = ToolCall(
run_id=run_id,
step_id=step.id if step else None,
tool_name=name,
arguments=arguments,
status="started",
)
session.add(trace)
await session.flush()
if unmet_dependencies:
evaluation = {
"verdict": "rejected",
"reason_code": "step_dependencies_not_satisfied",
"message": f"前置步骤尚未通过验收:{', '.join(unmet_dependencies)}",
}
trace.result = {"_evaluation": evaluation}
trace.status = "rejected"
step.checkpoint = {
**(step.checkpoint or {}),
"last_tool_call_id": trace.id,
"last_verdict": "rejected",
"unmet_dependencies": unmet_dependencies,
}
trace.duration_ms = int((time.perf_counter() - started) * 1000)
await session.commit()
return {
"error": "hard_constraint_failed",
"message": evaluation["message"],
"evaluation": evaluation,
}
if step:
step.status = "running"
step.checkpoint = {**(step.checkpoint or {}), "active_tool": name}
try:
result = None
max_retries = (
settings.agent_max_tool_retries
if tool.max_retries is None
else tool.max_retries
)
for attempt in range(max_retries + 1):
try:
timeout_seconds = tool.timeout_seconds or settings.agent_tool_timeout_seconds
result = await asyncio.wait_for(
tool.handler(session, arguments),
timeout=timeout_seconds,
)
break
except (TimeoutError, asyncio.TimeoutError) as exc:
if attempt >= max_retries:
raise TimeoutError(f"工具 {name} 超过 {timeout_seconds} 秒") from exc
except (ConnectionError, httpx.TransportError):
if attempt >= max_retries:
raise
serialized = json.loads(json.dumps(result, ensure_ascii=False, default=str))
evaluation = evaluate_tool_result(goal_spec or {}, name, serialized)
stored_result = (
{**serialized, "_evaluation": evaluation}
if isinstance(serialized, dict)
else {"items": serialized, "_evaluation": evaluation}
)
trace.result = stored_result
verdict = evaluation.get("verdict", "accepted")
trace.status = "rejected" if verdict == "rejected" else "completed"
if step:
step.status = (
"rejected"
if verdict == "rejected"
else ("running" if verdict == "progress" else "accepted")
)
step.result = {"tool": name, "evaluation": evaluation}
step.checkpoint = {
**(step.checkpoint or {}),
"last_tool_call_id": trace.id,
"last_verdict": verdict,
}
await self._sync_plan_status(session, run_id)
if verdict == "rejected":
rejection = {
"error": "hard_constraint_failed",
"message": evaluation.get("message", "工具结果未通过验收。"),
"evaluation": evaluation,
}
return {**result, **rejection} if isinstance(result, dict) else rejection
return result
except Exception as exc:
trace.status = "error"
trace.error = str(exc)[:1000]
if step:
step.status = "failed"
step.result = {"tool": name, "error": str(exc)[:1000]}
await self._sync_plan_status(session, run_id)
raise
finally:
trace.duration_ms = int((time.perf_counter() - started) * 1000)
await session.commit()
async def _resolve_step(
self,
session: AsyncSession,
run_id: str,
tool_name: str,
goal_spec: Dict[str, Any],
) -> Optional[AgentStep]:
if goal_spec.get("recipe") != "program_research":
return None
run = await session.get(AgentRun, run_id)
if not run:
return None
position = next(
(
index
for index, item in enumerate(run.plan or [])
if tool_name in (item.get("allowed_tools") or [])
),
None,
)
if position is None:
return None
return await session.scalar(
select(AgentStep).where(AgentStep.run_id == run_id, AgentStep.position == position)
)
async def _unmet_dependencies(
self,
session: AsyncSession,
run_id: str,
step: AgentStep,
) -> List[str]:
run = await session.get(AgentRun, run_id)
if not run or step.position >= len(run.plan or []):
return []
dependency_ids = set((run.plan or [])[step.position].get("dependencies") or [])
if not dependency_ids:
return []
steps = list(
(await session.scalars(select(AgentStep).where(AgentStep.run_id == run_id))).all()
)
by_plan_id = {
str((item.checkpoint or {}).get("plan_id") or f"step-{item.position + 1}"): item
for item in steps
}
return sorted(
dependency_id
for dependency_id in dependency_ids
if dependency_id not in by_plan_id
or by_plan_id[dependency_id].status not in {"accepted", "completed"}
)
async def _sync_plan_status(self, session: AsyncSession, run_id: str) -> None:
run = await session.get(AgentRun, run_id)
if not run:
return
steps = list(
(
await session.scalars(
select(AgentStep).where(AgentStep.run_id == run_id).order_by(AgentStep.position)
)
).all()
)
statuses = {step.position: step.status for step in steps}
run.plan = [
{**item, "status": statuses.get(index, item.get("status", "pending"))}
for index, item in enumerate(run.plan or [])
]
tool_registry = ToolRegistry()