MCPcopy Index your code
hub / github.com/AI45Lab/Code / parse_js_hook_response

Function parse_js_hook_response

sdk/node/src/lib.rs:5519–5557  ·  view source on GitHub ↗

Parse the return value from a JS hook callback into a `HookResponse`. Accepted JS return shapes: - `null` / `undefined` → continue - `{ action: 'continue' }` → continue - `{ action: 'block', reason: '…' }` → block - `{ action: 'skip' }` → skip - `{ action: 'retry', delayMs: N }` → retry after N ms

(val: napi::JsUnknown)

Source from the content-addressed store, hash-verified

5517/// - `{ action: 'skip' }` → skip
5518/// - `{ action: 'retry', delayMs: N }` → retry after N ms
5519fn parse_js_hook_response(val: napi::JsUnknown) -> napi::Result<RustHookResponse> {
5520 use napi::{JsObject, ValueType};
5521
5522 match val.get_type()? {
5523 ValueType::Null | ValueType::Undefined => Ok(RustHookResponse::continue_()),
5524 ValueType::Object => {
5525 let obj = unsafe { val.cast::<JsObject>() };
5526 let action: Option<String> = obj
5527 .get_named_property::<napi::JsString>("action")
5528 .ok()
5529 .and_then(|s| s.into_utf8().ok())
5530 .and_then(|s| s.into_owned().ok());
5531
5532 match action.as_deref() {
5533 Some("block") => {
5534 let reason = obj
5535 .get_named_property::<napi::JsString>("reason")
5536 .ok()
5537 .and_then(|s| s.into_utf8().ok())
5538 .and_then(|s| s.into_owned().ok())
5539 .unwrap_or_else(|| "Blocked by hook".to_string());
5540 Ok(RustHookResponse::block(reason))
5541 }
5542 Some("skip") => Ok(RustHookResponse::skip()),
5543 Some("retry") => {
5544 let delay_ms = obj
5545 .get_named_property::<napi::JsNumber>("delayMs")
5546 .ok()
5547 .and_then(|n| n.get_uint32().ok())
5548 .unwrap_or(1000) as u64;
5549 Ok(RustHookResponse::retry(delay_ms))
5550 }
5551 // "continue" or any other value → continue
5552 _ => Ok(RustHookResponse::continue_()),
5553 }
5554 }
5555 _ => Ok(RustHookResponse::continue_()),
5556 }
5557}
5558
5559// ============================================================================
5560// SkillInfo

Callers 1

handleMethod · 0.85

Calls 1

skipFunction · 0.85

Tested by

no test coverage detected