File size: 3,091 Bytes
5ca3c16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Version a built-in draft with one review; never edit published run snapshots."""
import copy
from src.modules.skill.domain.models import Draft


def with_code_review(body):
    result=copy.deepcopy(body)
    graph=result['graph']
    if any((n.get('operation_ref') or '').startswith('AO13@') for n in graph['nodes']):
        for node in graph['nodes']:
            if (node.get('operation_ref') or '').startswith('AO13@'):node['operation_ref']='AO13@0.2.0'
        return Draft.model_validate(result)
    if not any(e=={'source':'code','target':'patch'} for e in graph['control_edges']):
        raise ValueError('BUILTIN_REVIEW_INSERTION_POINT_MISSING')
    graph['nodes'].insert(next(i for i,n in enumerate(graph['nodes']) if n['id']=='patch'),
        {'id':'code_review','kind':'operation','operation_ref':'AO13@0.2.0'})
    graph['control_edges']=[e for e in graph['control_edges'] if e!={'source':'code','target':'patch'}]
    graph['control_edges'] += [{'source':'code','target':'code_review'},{'source':'code_review','target':'patch'}]
    for edge in graph['data_edges']:
        if edge['source']=='code' and edge['target']=='patch' and edge['target_port']=='patch':
            edge['source']='code_review'
    graph['data_edges'] += [dict(source='code',source_port='patch',target='code_review',target_port='patch'),
                           dict(source='design',source_port='design',target='code_review',target_port='design')]
    return Draft.model_validate(result)


def without_code_review(body):
    """Remove only the standard built-in review node, retaining all other graph data."""
    result=copy.deepcopy(body)
    graph=result['graph']
    nodes=[n for n in graph['nodes'] if (n.get('operation_ref') or '').startswith('AO13@')]
    if not nodes:
        return Draft.model_validate(result)
    if len(nodes)!=1 or nodes[0]['id']!='code_review':
        raise ValueError('BUILTIN_REVIEW_GRAPH_UNSUPPORTED')
    control=graph['control_edges']; data=graph['data_edges']
    expected_control=[{'source':'code','target':'code_review'},{'source':'code_review','target':'patch'}]
    expected_data=[dict(source='code',source_port='patch',target='code_review',target_port='patch'),
                   dict(source='design',source_port='design',target='code_review',target_port='design')]
    outgoing=dict(source='code_review',source_port='patch',target='patch',target_port='patch')
    if ([e for e in control if 'code_review' in (e['source'],e['target'])] != expected_control
            or any(e not in data for e in expected_data+[outgoing])
            or len([e for e in data if 'code_review' in (e['source'],e['target'])])!=3):
        raise ValueError('BUILTIN_REVIEW_GRAPH_UNSUPPORTED')
    graph['nodes'].remove(nodes[0])
    graph['control_edges']=[e for e in control if e not in expected_control]+[{'source':'code','target':'patch'}]
    graph['data_edges']=[e for e in data if e not in expected_data+[outgoing]]
    graph['data_edges'].append(dict(source='code',source_port='patch',target='patch',target_port='patch'))
    return Draft.model_validate(result)