File size: 4,170 Bytes
b27057a
 
 
 
 
 
 
76fcab1
b27057a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Runtime-owned source resolution. Files and retrieval evidence are separate inputs."""
import copy,hashlib
from KnowledgeService.parsing import extract,ParseError
from .models import RuntimeError
ACCEPT=('.txt','.md','.docx','.pdf')

def meeting_fields():
    return [{'name':'meeting_material','type':'file_or_text','required':True,'description':'会议原始材料','accept':list(ACCEPT),'max_files':5},{'name':'meeting_date','type':'date','required':False,'description':'会议日期;留空则相对期限待确认'}]

def prepare_material(workspace,user,wid,request,options=None,*,allow_inline=False):
    options=copy.deepcopy(options or {});ids=options.get('attachment_ids',[])
    allowed={a['attachment_id']:a for a in workspace.attachments(user,wid)['items']}
    if not ids and not options.get('source_text','').strip() and options.get('auto_attach',True):
        seen=set();ids=[]
        for a in allowed.values():
            if a['filename'].lower().endswith(ACCEPT) and a.get('parse_status')=='ready' and a['sha256'] not in seen:ids.append(a['attachment_id']);seen.add(a['sha256'])
        if allow_inline and not ids and len(request)>80 and '\n' in request:options['source_text']=request
    if len(ids)>5:raise RuntimeError(422,'MEETING_FILE_LIMIT','会议材料最多5个,请在启动卡片中选择文件。')
    unique=[];seen=set()
    for aid in ids:
        if aid not in allowed:raise RuntimeError(404,'RESOURCE_NOT_FOUND','会议附件不属于当前事项。')
        info=allowed[aid]
        if not info['filename'].lower().endswith(ACCEPT):raise RuntimeError(422,'MEETING_FILE_TYPE','请选择 TXT、Markdown、DOCX 或可提取文字的 PDF。')
        if info.get('parse_status')!='ready':raise RuntimeError(422,'MEETING_FILE_NOT_READY','会议文件未就绪或解析失败,请重新上传。')
        if info['sha256'] not in seen:unique.append(aid);seen.add(info['sha256'])
    if not unique and not options.get('source_text','').strip():raise RuntimeError(422,'WORKFLOW_INPUT_REQUIRED','请先在启动卡片中上传或选择会议文件,或粘贴会议原文;处理要求不能代替会议记录。',{'missing':['meeting_material'],'input_schema':meeting_fields()})
    options['attachment_ids']=unique
    return {'meeting_options':options,'material':read_material(workspace,user,wid,request,options)}

def read_material(workspace, user, wid, request, options):
    chunks = []; files = []
    allowed = {a['attachment_id']: a for a in workspace.attachments(user, wid)['items']}
    for aid in options.get('attachment_ids', []):
        if aid not in allowed:
            raise RuntimeError(404, 'RESOURCE_NOT_FOUND', '附件不属于当前事项。')
        info = allowed[aid]
        if not info['filename'].lower().endswith(('.txt', '.md', '.docx', '.pdf')):
            raise RuntimeError(422, 'MEETING_FILE_TYPE', '会议节点支持 TXT、Markdown、Word DOCX 和可提取文字的 PDF。')
        data = workspace.attachment_content(user, aid)
        try:
            # Parsing chunks overlap; reconstruct the original text without duplicated passages.
            parts = extract(info['filename'], data)
        except ParseError as exc:
            raise RuntimeError(422, 'MEETING_PARSE_FAILED', '会议文件解析失败。') from exc
        grouped = {}
        for locator, text, terms in parts:
            group = locator.split(' · 字符 ')[0]
            offset = int(locator.split(' · 字符 ')[1].split('–')[0]) - 1
            old = grouped.get(group, '')
            grouped[group] = old + text[max(0, len(old)-offset):]
        text = '\n'.join(grouped.values())
        chunks.append(text); files.append({'attachment_id': aid, 'filename': info['filename'],
            'sha256': hashlib.sha256(data).hexdigest(), 'characters': len(text)})
    text = '\n\n'.join(chunks) if chunks else options.get('source_text','')
    if not text.strip() or len(text) > 160000:
        raise RuntimeError(422, 'MEETING_TEXT_LIMIT', '会议文字须为 1–160000 字。')
    return {'text': text, 'files': files, 'instructions': request, 'source_sha256': hashlib.sha256(text.encode()).hexdigest()}