feat: casan chat optz
This commit is contained in:
@@ -14,6 +14,8 @@ import tempfile
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
|
||||
PYTHON_BIN = sys.executable or "/usr/bin/python3"
|
||||
|
||||
|
||||
def project_root() -> str:
|
||||
d = os.path.abspath(os.path.dirname(__file__))
|
||||
@@ -93,7 +95,7 @@ def sha_file(path: str) -> str:
|
||||
|
||||
|
||||
def classify(message: str):
|
||||
r = subprocess.run(["python3", ROUTER, "classify", "--message", message], cwd=ROOT, capture_output=True, text=True)
|
||||
r = subprocess.run([PYTHON_BIN, ROUTER, "classify", "--message", message], cwd=ROOT, capture_output=True, text=True)
|
||||
try:
|
||||
return json.loads(r.stdout)
|
||||
except Exception:
|
||||
@@ -332,8 +334,8 @@ def certify_operator_draft(args, router, binding):
|
||||
"--cost-per-step", "0",
|
||||
"--context", draft_path,
|
||||
], cwd=ROOT, capture_output=True, text=True, env=loop_env)
|
||||
trace_rc = subprocess.run(["python3", LOOP_TRACE, "verify-chain", "--run-id", run_id], cwd=ROOT, capture_output=True, text=True, env=loop_env)
|
||||
replay_rc = subprocess.run(["python3", LOOP_TRACE, "replay", "--run-id", run_id, "--profile", os.environ.get("CASAN_PROFILE", "dev")], cwd=ROOT, capture_output=True, text=True, env=loop_env)
|
||||
trace_rc = subprocess.run([PYTHON_BIN, LOOP_TRACE, "verify-chain", "--run-id", run_id], cwd=ROOT, capture_output=True, text=True, env=loop_env)
|
||||
replay_rc = subprocess.run([PYTHON_BIN, LOOP_TRACE, "replay", "--run-id", run_id, "--profile", os.environ.get("CASAN_PROFILE", "dev")], cwd=ROOT, capture_output=True, text=True, env=loop_env)
|
||||
certified = r.returncode == 0 and "final=DONE" in r.stdout and trace_rc.returncode == 0 and replay_rc.returncode == 0
|
||||
return {
|
||||
"success": certified,
|
||||
@@ -492,8 +494,8 @@ def certify_codegen_draft(args, router, binding):
|
||||
"--cost-per-step", "0",
|
||||
"--context", artifact_path,
|
||||
], cwd=ROOT, capture_output=True, text=True, env=loop_env)
|
||||
trace_rc = subprocess.run(["python3", LOOP_TRACE, "verify-chain", "--run-id", run_id], cwd=ROOT, capture_output=True, text=True, env=loop_env)
|
||||
replay_rc = subprocess.run(["python3", LOOP_TRACE, "replay", "--run-id", run_id, "--profile", os.environ.get("CASAN_PROFILE", "dev")], cwd=ROOT, capture_output=True, text=True, env=loop_env)
|
||||
trace_rc = subprocess.run([PYTHON_BIN, LOOP_TRACE, "verify-chain", "--run-id", run_id], cwd=ROOT, capture_output=True, text=True, env=loop_env)
|
||||
replay_rc = subprocess.run([PYTHON_BIN, LOOP_TRACE, "replay", "--run-id", run_id, "--profile", os.environ.get("CASAN_PROFILE", "dev")], cwd=ROOT, capture_output=True, text=True, env=loop_env)
|
||||
output_scan_rc, output_scan_msg = scan_tool_output(draft, "chat-codegen-output")
|
||||
certified = (
|
||||
r.returncode == 0
|
||||
@@ -602,7 +604,7 @@ def bind_agent(args, router):
|
||||
if router.get("mode") == "CODEGEN":
|
||||
tools.extend(["artifact-scan", "tool-output-scan"])
|
||||
cmd = [
|
||||
"python3", AGENT_RESOLVER, "bind",
|
||||
PYTHON_BIN, AGENT_RESOLVER, "bind",
|
||||
"--agent", agent,
|
||||
"--actor", args.actor,
|
||||
"--role", args.role,
|
||||
@@ -627,7 +629,7 @@ def bind_agent(args, router):
|
||||
|
||||
def bind_model(args, binding):
|
||||
r = subprocess.run([
|
||||
"python3", MODEL_RESOLVER, "bind",
|
||||
PYTHON_BIN, MODEL_RESOLVER, "bind",
|
||||
"--provider", args.model_provider,
|
||||
"--actor", args.actor,
|
||||
"--role", args.role,
|
||||
@@ -654,7 +656,7 @@ def submit_escalation(args, router, binding):
|
||||
}
|
||||
reason = f"chat turn requires approval: {binding.get('reason', 'requires_approval')}"
|
||||
r = subprocess.run([
|
||||
"python3", APPROVAL_INBOX, "submit",
|
||||
PYTHON_BIN, APPROVAL_INBOX, "submit",
|
||||
"--project", args.project,
|
||||
"--action", "chat.escalate",
|
||||
"--target", f"chat:{args.chat_id or 'chat-default'}:{turn_id}",
|
||||
@@ -730,23 +732,23 @@ def ask(args) -> int:
|
||||
if loop_rc != 0:
|
||||
denied_from_loop(router, binding, loop_run)
|
||||
return loop_rc
|
||||
return run_mode(["python3", OPERATOR, "run", *common], binding, loop_run, "chat-operator")
|
||||
return run_mode([PYTHON_BIN, OPERATOR, "run", *common], binding, loop_run, "chat-operator")
|
||||
if router.get("mode") == "CODEGEN":
|
||||
loop_run, loop_rc = certify_codegen_draft(args, router, binding)
|
||||
return finish_codegen(args, router, binding, loop_run, loop_rc)
|
||||
if getattr(args, "stream", False) and router.get("mode") in ("READ_ONLY", "ANALYSIS"):
|
||||
env = os.environ.copy()
|
||||
env["CASAN_CHAT_MODEL_PROVIDER"] = model_binding.get("provider", "")
|
||||
return run_and_passthrough(["python3", READONLY, "ask", "--stream", *common], env)
|
||||
return run_mode(["python3", READONLY, "ask", *common], binding)
|
||||
return run_and_passthrough([PYTHON_BIN, READONLY, "ask", "--stream", *common], env)
|
||||
return run_mode([PYTHON_BIN, READONLY, "ask", *common], binding)
|
||||
|
||||
|
||||
def verify_audit() -> int:
|
||||
return run_and_passthrough(["python3", READONLY, "verify-audit"])
|
||||
return run_and_passthrough([PYTHON_BIN, READONLY, "verify-audit"])
|
||||
|
||||
|
||||
def history(args) -> int:
|
||||
command = ["python3", READONLY, "history", "--actor", args.actor, "--tenant", args.tenant, "--limit", str(args.limit)]
|
||||
command = [PYTHON_BIN, READONLY, "history", "--actor", args.actor, "--tenant", args.tenant, "--limit", str(args.limit)]
|
||||
if args.chat_id:
|
||||
command += ["--chat-id", args.chat_id]
|
||||
return run_and_passthrough(command)
|
||||
|
||||
Reference in New Issue
Block a user