| 1 |
|
| /* pmcp_call.h - tools/list and tools/call. |
| 2 |
|
| * |
| 3 |
|
| * Needs: pmcp_decl.h, pmcp_page.h. |
| 4 |
|
| * |
| 5 |
|
| * A tool call runs the declared action the way a route runs it: a context of |
| 6 |
|
| * the request's class, built with Punk::Context->_build over the request's env |
| 7 |
|
| * and app, holding a copy of the request's stash (so what `auth` stashed is |
| 8 |
|
| * there) and the arguments as its OpenAPI params (so $c->param reads them). |
| 9 |
|
| * The action cannot tell a tool call from a web call except through $c->mcp. |
| 10 |
|
| * |
| 11 |
|
| * Whatever the action returns is turned into a tool result here. A die is a |
| 12 |
|
| * result with isError, not a protocol error: the call was well formed and that |
| 13 |
|
| * is its answer, and the model is the one that has to read it. |
| 14 |
|
| */ |
| 15 |
|
| |
| 16 |
|
| #ifndef PMCP_CALL_H |
| 17 |
|
| #define PMCP_CALL_H |
| 18 |
|
| |
| 19 |
|
| #define PMCP_CALL_CLASS "Punk::MCP::Call" |
| 20 |
|
| #define PMCP_STASH_CALL "mcp.call" |
| 21 |
|
| #define PMCP_REFUSAL_MAX 300 |
| 22 |
|
| |
| 23 |
|
| /* Defined in pmcp_sse.h: tell a call object about the stream it runs in. */ |
| 24 |
|
| static void pmcp_call_attach(pTHX_ pmcp_req *r, SV *call); |
| 25 |
|
| /* Defined in pmcp_oa.h: an OpenAPI tool's request, answered by the app. */ |
| 26 |
|
| static SV *pmcp_oa_call(pTHX_ SV *psgi, pmcp_tool *t, SV *c, HV *args, SV **why); |
| 27 |
|
| |
| 28 |
|
| /* ---- small builders --------------------------------------------------------- */ |
| 29 |
|
| |
| 30 |
|
| /* [{type => 'text', text => TEXT}] +1 */ |
| 31 |
117 |
| static SV *pmcp_text_content(pTHX_ SV *text) |
| 32 |
|
| { |
| 33 |
117 |
| AV *a = newAV(); |
| 34 |
117 |
| HV *b = newHV(); |
| 35 |
117 |
| (void)hv_stores(b, "type", newSVpvs("text")); |
| 36 |
117 |
| (void)hv_stores(b, "text", text); |
| 37 |
117 |
| av_push(a, newRV_noinc((SV *)b)); |
| 38 |
117 |
| return newRV_noinc((SV *)a); |
| 39 |
|
| } |
| 40 |
|
| |
| 41 |
|
| /* {content => [text], isError => true}, plus structuredContent when given and |
| 42 |
|
| * the version has it. Takes ownership of text and structured. +1 */ |
| 43 |
21 |
| static SV *pmcp_tool_error(pTHX_ SV *text, SV *structured, enum pmcp_ver v) |
| 44 |
|
| { |
| 45 |
21 |
| HV *r = newHV(); |
| 46 |
21 |
| (void)hv_stores(r, "content", pmcp_text_content(aTHX_ text)); |
| 47 |
21 |
| (void)hv_stores(r, "isError", pmcp_bool(aTHX_ 1)); |
| 48 |
21 |
▶ | if (structured && v >= PMCP_V_2025_06_18) |
Branch: 2 branches
| Branch | True | False | Total |
| if (structured && v >= PMCP_V_2025_06_18) |
4 |
17 |
21 |
| if (structured && v >= PMCP_V_2025_06_18) |
4 |
0 |
4 |
|
| 49 |
4 |
| (void)hv_stores(r, "structuredContent", structured); |
| 50 |
17 |
▶ | else if (structured) SvREFCNT_dec(structured); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (structured) SvREFCNT_dec(structured); |
0 |
17 |
17 |
|
| 51 |
21 |
| return newRV_noinc((SV *)r); |
| 52 |
|
| } |
| 53 |
|
| |
| 54 |
|
| /* A die's text, without the " at FILE line N." perl added, which names this |
| 55 |
|
| * dist's caller rather than anything the model can use. +1 */ |
| 56 |
14 |
| static SV *pmcp_die_text(pTHX_ SV *err) |
| 57 |
|
| { |
| 58 |
|
| SV *t; |
| 59 |
|
| STRLEN l; |
| 60 |
|
| const char *p; |
| 61 |
14 |
▶ | if (SvROK(err) && !SvAMAGIC(err)) return newSVpvs("the tool failed"); |
Branch: 4 branches
| Branch | True | False | Total |
| if (SvROK(err) && !SvAMAGIC(err)) return newSVpvs("the tool failed"); |
1 |
13 |
14 |
| if (SvROK(err) && !SvAMAGIC(err)) return newSVpvs("the tool failed"); |
1 |
0 |
1 |
| if (SvROK(err) && !SvAMAGIC(err)) return newSVpvs("the tool failed"); |
0 |
1 |
1 |
| if (SvROK(err) && !SvAMAGIC(err)) return newSVpvs("the tool failed"); |
0 |
0 |
0 |
|
| 62 |
13 |
| t = newSVsv(err); |
| 63 |
13 |
| p = SvPV_const(t, l); |
| 64 |
26 |
▶ | while (l && (p[l - 1] == '\n' || p[l - 1] == ' ')) l--; |
Branch: 3 branches
| Branch | True | False | Total |
| while (l && (p[l - 1] == '\n' || p[l - 1] == ' ')) l--; |
26 |
0 |
26 |
| while (l && (p[l - 1] == '\n' || p[l - 1] == ' ')) l--; |
13 |
13 |
26 |
| while (l && (p[l - 1] == '\n' || p[l - 1] == ' ')) l--; |
0 |
13 |
13 |
|
| 65 |
|
| /* the last " at ", if what follows it is "FILE line N." */ |
| 66 |
13 |
▶ | if (l && p[l - 1] == '.') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (l && p[l - 1] == '.') { |
13 |
0 |
13 |
| if (l && p[l - 1] == '.') { |
3 |
10 |
13 |
|
| 67 |
3 |
| STRLEN i = l; |
| 68 |
171 |
▶ | while (i >= 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i >= 4) { |
171 |
0 |
171 |
|
| 69 |
171 |
| i--; |
| 70 |
171 |
▶ | if (memEQ(p + i - 3, " at ", 4)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (memEQ(p + i - 3, " at ", 4)) { |
3 |
168 |
171 |
|
| 71 |
3 |
| const char *tail = p + i + 1; |
| 72 |
3 |
| STRLEN tl = l - (i + 1); |
| 73 |
|
| static const char ln[] = " line "; |
| 74 |
3 |
▶ | if (tl < 300 && ninstr(tail, tail + tl, ln, ln + 6)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (tl < 300 && ninstr(tail, tail + tl, ln, ln + 6)) |
3 |
0 |
3 |
| if (tl < 300 && ninstr(tail, tail + tl, ln, ln + 6)) |
3 |
0 |
3 |
|
| 75 |
3 |
| l = i - 3; |
| 76 |
3 |
| break; |
| 77 |
|
| } |
| 78 |
|
| } |
| 79 |
|
| } |
| 80 |
13 |
| SvCUR_set(t, l); |
| 81 |
13 |
| return t; |
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
|
| /* ---- the call object -------------------------------------------------------- */ |
| 85 |
|
| |
| 86 |
174 |
| static SV *pmcp_call_object(pTHX_ const char *method, SV *name, SV *args, |
| 87 |
|
| enum pmcp_ver v, HV *params) |
| 88 |
|
| { |
| 89 |
174 |
| HV *h = newHV(); |
| 90 |
174 |
▶ | SV **meta = params ? hv_fetchs(params, "_meta", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **meta = params ? hv_fetchs(params, "_meta", 0) : NULL; |
174 |
0 |
174 |
|
| 91 |
174 |
| (void)hv_stores(h, "method", newSVpv(method, 0)); |
| 92 |
174 |
▶ | if (name) (void)hv_stores(h, "name", newSVsv(name)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (name) (void)hv_stores(h, "name", newSVsv(name)); |
174 |
0 |
174 |
|
| 93 |
174 |
▶ | (void)hv_stores(h, "arguments", args ? newSVsv(args) : newSV(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_stores(h, "arguments", args ? newSVsv(args) : newSV(0)); |
174 |
0 |
174 |
|
| 94 |
174 |
▶ | (void)hv_stores(h, "protocol", |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_stores(h, "protocol", |
174 |
0 |
174 |
|
| 95 |
|
| v ? newSVpv(PMCP_VER_NAME[v], 0) : newSV(0)); |
| 96 |
174 |
▶ | if (meta && pmcp_is_hash(*meta)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (meta && pmcp_is_hash(*meta)) { |
9 |
165 |
174 |
| if (meta && pmcp_is_hash(*meta)) { |
9 |
0 |
9 |
|
| 97 |
9 |
| SV **pt = hv_fetchs((HV *)SvRV(*meta), "progressToken", 0); |
| 98 |
9 |
▶ | if (pt && pmcp_id_ok(*pt)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (pt && pmcp_id_ok(*pt)) |
9 |
0 |
9 |
| if (pt && pmcp_id_ok(*pt)) |
9 |
0 |
9 |
|
| 99 |
9 |
| (void)hv_stores(h, "progress_token", newSVsv(*pt)); |
| 100 |
|
| } |
| 101 |
174 |
| (void)hv_stores(h, "cancelled", newSViv(0)); |
| 102 |
174 |
| return sv_bless(newRV_noinc((SV *)h), gv_stashpvs(PMCP_CALL_CLASS, GV_ADD)); |
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
|
| /* ---- the child context --------------------------------------------------------- */ |
| 106 |
|
| |
| 107 |
531 |
| static SV *pmcp_meth0(pTHX_ SV *obj, const char *m) |
| 108 |
|
| { |
| 109 |
531 |
| return pmcp_call_method(aTHX_ obj, m, NULL, 0, G_SCALAR, NULL); |
| 110 |
|
| } |
| 111 |
|
| |
| 112 |
|
| /* A context for one call, as described above. +1 */ |
| 113 |
174 |
| static SV *pmcp_child_context(pTHX_ SV *c, const char *kind, SV *name, |
| 114 |
|
| SV *args, SV *call) |
| 115 |
|
| { |
| 116 |
174 |
| SV *env = sv_2mortal(pmcp_meth0(aTHX_ c, "env")); |
| 117 |
174 |
| SV *app = sv_2mortal(pmcp_meth0(aTHX_ c, "app")); |
| 118 |
174 |
| SV *stash = sv_2mortal(pmcp_meth0(aTHX_ c, "stash")); |
| 119 |
174 |
| HV *match = newHV(), *op = newHV(), *copy; |
| 120 |
|
| SV *argv[3], *child, *r; |
| 121 |
174 |
▶ | const char *class = HvNAME(SvSTASH(SvRV(c))); |
Branch: 6 branches
| Branch | True | False | Total |
| const char *class = HvNAME(SvSTASH(SvRV(c))); |
174 |
0 |
174 |
| const char *class = HvNAME(SvSTASH(SvRV(c))); |
174 |
0 |
174 |
| const char *class = HvNAME(SvSTASH(SvRV(c))); |
0 |
174 |
174 |
| const char *class = HvNAME(SvSTASH(SvRV(c))); |
0 |
0 |
0 |
| const char *class = HvNAME(SvSTASH(SvRV(c))); |
174 |
0 |
174 |
| const char *class = HvNAME(SvSTASH(SvRV(c))); |
0 |
174 |
174 |
|
| 122 |
|
| |
| 123 |
174 |
| (void)hv_stores(match, "captures", newRV_noinc((SV *)newHV())); |
| 124 |
174 |
| (void)hv_store(match, kind, (I32)strlen(kind), newSVsv(name), 0); |
| 125 |
174 |
| argv[0] = env; |
| 126 |
174 |
| argv[1] = app; |
| 127 |
174 |
| argv[2] = sv_2mortal(newRV_noinc((SV *)match)); |
| 128 |
174 |
| child = pmcp_call_method(aTHX_ sv_2mortal(newSVpv(class, 0)), "_build", |
| 129 |
|
| argv, 3, G_SCALAR, NULL); |
| 130 |
|
| |
| 131 |
174 |
▶ | copy = pmcp_is_hash(stash) ? newHVhv((HV *)SvRV(stash)) : newHV(); |
Branch: 1 branch
| Branch | True | False | Total |
| copy = pmcp_is_hash(stash) ? newHVhv((HV *)SvRV(stash)) : newHV(); |
174 |
0 |
174 |
|
| 132 |
174 |
| (void)hv_stores(copy, PMCP_STASH_CALL, newSVsv(call)); |
| 133 |
174 |
| argv[0] = sv_2mortal(newRV_noinc((SV *)copy)); |
| 134 |
174 |
| r = pmcp_call_method(aTHX_ child, "stash_hv", argv, 1, G_SCALAR, NULL); |
| 135 |
174 |
| SvREFCNT_dec(r); |
| 136 |
|
| |
| 137 |
174 |
| (void)hv_stores(op, "query", newSVsv(args)); |
| 138 |
174 |
| argv[0] = sv_2mortal(newRV_noinc((SV *)op)); |
| 139 |
174 |
| r = pmcp_call_method(aTHX_ child, "openapi_params", argv, 1, G_SCALAR, NULL); |
| 140 |
174 |
| SvREFCNT_dec(r); |
| 141 |
174 |
| return child; |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
|
| /* ---- guards ----------------------------------------------------------------- */ |
| 145 |
|
| |
| 146 |
|
| /* A refusal's words: the body of the response a guard returned, if it is short |
| 147 |
|
| * text, with the status. +1 */ |
| 148 |
21 |
| static SV *pmcp_refusal_text(pTHX_ SV *resp) |
| 149 |
|
| { |
| 150 |
21 |
| SV *out = newSVpvs("refused"); |
| 151 |
21 |
▶ | if (pmcp_is_array(resp) && av_len((AV *)SvRV(resp)) >= 2) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (pmcp_is_array(resp) && av_len((AV *)SvRV(resp)) >= 2) { |
21 |
0 |
21 |
| if (pmcp_is_array(resp) && av_len((AV *)SvRV(resp)) >= 2) { |
21 |
0 |
21 |
|
| 152 |
21 |
| AV *t = (AV *)SvRV(resp); |
| 153 |
21 |
| SV **st = av_fetch(t, 0, 0), **bd = av_fetch(t, 2, 0); |
| 154 |
21 |
▶ | if (st && SvOK(*st)) sv_catpvf(out, " (%" IVdf ")", SvIV(*st)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (st && SvOK(*st)) sv_catpvf(out, " (%" IVdf ")", SvIV(*st)); |
21 |
0 |
21 |
| if (st && SvOK(*st)) sv_catpvf(out, " (%" IVdf ")", SvIV(*st)); |
21 |
0 |
21 |
|
| 155 |
21 |
▶ | if (bd && pmcp_is_array(*bd)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (bd && pmcp_is_array(*bd)) { |
21 |
0 |
21 |
| if (bd && pmcp_is_array(*bd)) { |
21 |
0 |
21 |
|
| 156 |
21 |
| AV *b = (AV *)SvRV(*bd); |
| 157 |
21 |
| SSize_t i, n = av_len(b) + 1; |
| 158 |
21 |
| SV *body = sv_2mortal(newSVpvs("")); |
| 159 |
42 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
21 |
21 |
42 |
|
| 160 |
21 |
| SV **e = av_fetch(b, i, 0); |
| 161 |
21 |
▶ | if (e && SvOK(*e) && !SvROK(*e)) sv_catsv(body, *e); |
Branch: 3 branches
| Branch | True | False | Total |
| if (e && SvOK(*e) && !SvROK(*e)) sv_catsv(body, *e); |
21 |
0 |
21 |
| if (e && SvOK(*e) && !SvROK(*e)) sv_catsv(body, *e); |
21 |
0 |
21 |
| if (e && SvOK(*e) && !SvROK(*e)) sv_catsv(body, *e); |
21 |
0 |
21 |
|
| 162 |
|
| } |
| 163 |
21 |
▶ | if (SvCUR(body) && SvCUR(body) <= PMCP_REFUSAL_MAX) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvCUR(body) && SvCUR(body) <= PMCP_REFUSAL_MAX) { |
21 |
0 |
21 |
| if (SvCUR(body) && SvCUR(body) <= PMCP_REFUSAL_MAX) { |
21 |
0 |
21 |
|
| 164 |
21 |
| sv_catpvs(out, ": "); |
| 165 |
21 |
| sv_catsv(out, body); |
| 166 |
|
| } |
| 167 |
|
| } |
| 168 |
|
| } |
| 169 |
21 |
| return out; |
| 170 |
|
| } |
| 171 |
|
| |
| 172 |
|
| /* Run a declaration's guards over `ctx`. NULL when every one passes, else the |
| 173 |
|
| * reason (+1): a refusal's words, or a die's text. */ |
| 174 |
204 |
| static SV *pmcp_run_guards(pTHX_ AV *guards, SV *ctx) |
| 175 |
|
| { |
| 176 |
|
| SSize_t i, n; |
| 177 |
204 |
▶ | if (!guards) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!guards) return NULL; |
168 |
36 |
204 |
|
| 178 |
36 |
| n = av_len(guards) + 1; |
| 179 |
55 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
43 |
12 |
55 |
|
| 180 |
43 |
| SV *g = *av_fetch(guards, i, 0), *err = NULL, *r; |
| 181 |
43 |
| r = pmcp_call_sv(aTHX_ g, &ctx, 1, G_SCALAR, &err); |
| 182 |
43 |
▶ | if (!r) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) { |
4 |
39 |
43 |
|
| 183 |
4 |
| SV *t = pmcp_die_text(aTHX_ err); |
| 184 |
4 |
| SvREFCNT_dec(err); |
| 185 |
24 |
| return t; |
| 186 |
|
| } |
| 187 |
39 |
▶ | if (SvROK(r)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(r)) { |
20 |
19 |
39 |
|
| 188 |
20 |
| SV *t = pmcp_refusal_text(aTHX_ r); |
| 189 |
20 |
| SvREFCNT_dec(r); |
| 190 |
20 |
| return t; |
| 191 |
|
| } |
| 192 |
19 |
| SvREFCNT_dec(r); |
| 193 |
|
| } |
| 194 |
12 |
| return NULL; |
| 195 |
|
| } |
| 196 |
|
| |
| 197 |
|
| /* ---- tools/list ------------------------------------------------------------- */ |
| 198 |
|
| |
| 199 |
|
| typedef struct { pmcp_tools *ts; SV *c; } pmcp_list_ctx; |
| 200 |
|
| |
| 201 |
32 |
| static int pmcp_tool_visible(pTHX_ void *vctx, IV i) |
| 202 |
|
| { |
| 203 |
32 |
| pmcp_list_ctx *x = (pmcp_list_ctx *)vctx; |
| 204 |
|
| SV *why; |
| 205 |
32 |
▶ | if (!x->ts->t[i].guards) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!x->ts->t[i].guards) return 1; |
12 |
20 |
32 |
|
| 206 |
20 |
▶ | if (!x->c) return 0; /* no request to judge it by: hidden */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!x->c) return 0; /* no request to judge it by: hidden */ |
0 |
20 |
20 |
|
| 207 |
20 |
| why = pmcp_run_guards(aTHX_ x->ts->t[i].guards, x->c); |
| 208 |
20 |
▶ | if (!why) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!why) return 1; |
8 |
12 |
20 |
|
| 209 |
12 |
| SvREFCNT_dec(why); |
| 210 |
12 |
| return 0; |
| 211 |
|
| } |
| 212 |
|
| |
| 213 |
174 |
| static enum pmcp_ver pmcp_req_version(const pmcp_req *r) |
| 214 |
|
| { |
| 215 |
174 |
▶ | return r->version ? r->version : PMCP_V_2025_03_26; |
Branch: 1 branch
| Branch | True | False | Total |
| return r->version ? r->version : PMCP_V_2025_03_26; |
174 |
0 |
174 |
|
| 216 |
|
| } |
| 217 |
|
| |
| 218 |
40 |
| static SV *pmcp_tools_list(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params) |
| 219 |
|
| { |
| 220 |
40 |
| pmcp_tools *ts = (pmcp_tools *)s->tools; |
| 221 |
40 |
| enum pmcp_ver v = pmcp_req_version(r); |
| 222 |
40 |
▶ | SV **cur = params ? hv_fetchs(params, "cursor", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **cur = params ? hv_fetchs(params, "cursor", 0) : NULL; |
26 |
14 |
40 |
|
| 223 |
40 |
| IV start = 0; |
| 224 |
|
| pmcp_list_ctx x; |
| 225 |
|
| |
| 226 |
40 |
▶ | if (!ts) return pmcp_fail(r, PMCP_METHOD, "method not found"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ts) return pmcp_fail(r, PMCP_METHOD, "method not found"); |
0 |
40 |
40 |
|
| 227 |
40 |
▶ | if (cur && SvOK(*cur)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (cur && SvOK(*cur)) { |
16 |
24 |
40 |
| if (cur && SvOK(*cur)) { |
16 |
0 |
16 |
|
| 228 |
16 |
| start = pmcp_cursor_read(aTHX_ *cur, ts->n); |
| 229 |
16 |
▶ | if (start < 0) return pmcp_fail(r, PMCP_PARAMS, "invalid cursor"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (start < 0) return pmcp_fail(r, PMCP_PARAMS, "invalid cursor"); |
9 |
7 |
16 |
|
| 230 |
|
| } |
| 231 |
31 |
| x.ts = ts; |
| 232 |
31 |
| x.c = r->c; |
| 233 |
31 |
| r->raw = pmcp_page_bytes(aTHX_ "tools", ts->list[v], ts->n, start, |
| 234 |
|
| ts->page_size, |
| 235 |
31 |
▶ | ts->any_guard ? pmcp_tool_visible : NULL, &x); |
Branch: 1 branch
| Branch | True | False | Total |
| ts->any_guard ? pmcp_tool_visible : NULL, &x); |
9 |
22 |
31 |
|
| 236 |
31 |
| return NULL; |
| 237 |
|
| } |
| 238 |
|
| |
| 239 |
|
| /* ---- turning what the action returned into a result ----------------------------- */ |
| 240 |
|
| |
| 241 |
104 |
| static int pmcp_is_triplet(pTHX_ SV *sv) |
| 242 |
|
| { |
| 243 |
|
| AV *a; |
| 244 |
|
| SV **st, **h, **b; |
| 245 |
104 |
▶ | if (!pmcp_is_array(sv)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pmcp_is_array(sv)) return 0; |
67 |
37 |
104 |
|
| 246 |
37 |
| a = (AV *)SvRV(sv); |
| 247 |
37 |
▶ | if (av_len(a) != 2) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(a) != 2) return 0; |
6 |
31 |
37 |
|
| 248 |
31 |
| st = av_fetch(a, 0, 0); |
| 249 |
31 |
| h = av_fetch(a, 1, 0); |
| 250 |
31 |
| b = av_fetch(a, 2, 0); |
| 251 |
31 |
▶ | return st && SvOK(*st) && !SvROK(*st) && looks_like_number(*st) |
Branch: 3 branches
| Branch | True | False | Total |
| return st && SvOK(*st) && !SvROK(*st) && looks_like_number(*st) |
31 |
0 |
31 |
| return st && SvOK(*st) && !SvROK(*st) && looks_like_number(*st) |
31 |
0 |
31 |
| return st && SvOK(*st) && !SvROK(*st) && looks_like_number(*st) |
31 |
0 |
31 |
|
| 252 |
31 |
▶ | && SvIV(*st) >= 100 && SvIV(*st) <= 599 |
Branch: 2 branches
| Branch | True | False | Total |
| && SvIV(*st) >= 100 && SvIV(*st) <= 599 |
31 |
0 |
31 |
| && SvIV(*st) >= 100 && SvIV(*st) <= 599 |
31 |
0 |
31 |
|
| 253 |
62 |
▶ | && h && pmcp_is_array(*h) && b && SvROK(*b); |
Branch: 5 branches
| Branch | True | False | Total |
| && h && pmcp_is_array(*h) && b && SvROK(*b); |
31 |
0 |
31 |
| && h && pmcp_is_array(*h) && b && SvROK(*b); |
31 |
0 |
31 |
| && h && pmcp_is_array(*h) && b && SvROK(*b); |
31 |
0 |
31 |
| && h && pmcp_is_array(*h) && b && SvROK(*b); |
31 |
0 |
31 |
| && h && pmcp_is_array(*h) && b && SvROK(*b); |
31 |
0 |
31 |
|
| 254 |
|
| } |
| 255 |
|
| |
| 256 |
|
| /* A triplet's body as one string, or NULL when it is a filehandle. Mortal. */ |
| 257 |
31 |
| static SV *pmcp_triplet_body(pTHX_ AV *t, int *is_json) |
| 258 |
|
| { |
| 259 |
31 |
| SV **h = av_fetch(t, 1, 0), **b = av_fetch(t, 2, 0); |
| 260 |
31 |
| AV *hd = (AV *)SvRV(*h); |
| 261 |
31 |
| SSize_t i, n = av_len(hd) + 1; |
| 262 |
|
| SV *body; |
| 263 |
31 |
| *is_json = 0; |
| 264 |
90 |
▶ | for (i = 0; i + 1 < n; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 1 < n; i += 2) { |
59 |
31 |
90 |
|
| 265 |
59 |
| SV **k = av_fetch(hd, i, 0), **v = av_fetch(hd, i + 1, 0); |
| 266 |
118 |
▶ | if (k && v && SvOK(*k) && strcasecmp(SvPV_nolen(*k), "Content-Type") == 0 |
Branch: 4 branches
| Branch | True | False | Total |
| if (k && v && SvOK(*k) && strcasecmp(SvPV_nolen(*k), "Content-Type") == 0 |
59 |
0 |
59 |
| if (k && v && SvOK(*k) && strcasecmp(SvPV_nolen(*k), "Content-Type") == 0 |
59 |
0 |
59 |
| if (k && v && SvOK(*k) && strcasecmp(SvPV_nolen(*k), "Content-Type") == 0 |
59 |
0 |
59 |
| if (k && v && SvOK(*k) && strcasecmp(SvPV_nolen(*k), "Content-Type") == 0 |
30 |
29 |
59 |
|
| 267 |
60 |
▶ | && instr(SvPV_nolen(*v), "json")) |
Branch: 1 branch
| Branch | True | False | Total |
| && instr(SvPV_nolen(*v), "json")) |
21 |
9 |
30 |
|
| 268 |
21 |
| *is_json = 1; |
| 269 |
|
| } |
| 270 |
31 |
▶ | if (!pmcp_is_array(*b)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pmcp_is_array(*b)) return NULL; |
0 |
31 |
31 |
|
| 271 |
31 |
| body = sv_2mortal(newSVpvs("")); |
| 272 |
31 |
| n = av_len((AV *)SvRV(*b)) + 1; |
| 273 |
61 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
30 |
31 |
61 |
|
| 274 |
30 |
| SV **e = av_fetch((AV *)SvRV(*b), i, 0); |
| 275 |
30 |
▶ | if (e && SvOK(*e) && !SvROK(*e)) sv_catsv(body, *e); |
Branch: 3 branches
| Branch | True | False | Total |
| if (e && SvOK(*e) && !SvROK(*e)) sv_catsv(body, *e); |
30 |
0 |
30 |
| if (e && SvOK(*e) && !SvROK(*e)) sv_catsv(body, *e); |
30 |
0 |
30 |
| if (e && SvOK(*e) && !SvROK(*e)) sv_catsv(body, *e); |
30 |
0 |
30 |
|
| 276 |
|
| } |
| 277 |
31 |
| return body; |
| 278 |
|
| } |
| 279 |
|
| |
| 280 |
|
| /* The result for a value. *err is set (+1) for a value that cannot be a |
| 281 |
|
| * result at all, which the caller answers -32603. +1 */ |
| 282 |
|
| static SV *pmcp_result_of(pTHX_ pmcp_tool *t, SV *val, enum pmcp_ver v, |
| 283 |
|
| int depth, SV **err); |
| 284 |
|
| |
| 285 |
|
| /* A Future, awaited on the child context, within the tool's timeout. +1 */ |
| 286 |
5 |
| static SV *pmcp_await_result(pTHX_ pmcp_tool *t, SV *child, SV *f, |
| 287 |
|
| enum pmcp_ver v, int depth, SV **err) |
| 288 |
|
| { |
| 289 |
5 |
| SV *wait = f, *e = NULL, *vals, *val; |
| 290 |
5 |
| int ready_now = 0; |
| 291 |
|
| { |
| 292 |
5 |
| SV *r = pmcp_meth0(aTHX_ f, "is_ready"); |
| 293 |
5 |
▶ | ready_now = r && SvTRUE(r); |
Branch: 2 branches
| Branch | True | False | Total |
| ready_now = r && SvTRUE(r); |
5 |
0 |
5 |
| ready_now = r && SvTRUE(r); |
4 |
1 |
5 |
|
| 294 |
5 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
5 |
0 |
5 |
|
| 295 |
|
| } |
| 296 |
|
| /* Only a Future still pending races the timer. Off a Hyperman worker |
| 297 |
|
| * $c->timer sleeps when it is made, so racing a settled Future would cost |
| 298 |
|
| * the whole timeout for nothing. */ |
| 299 |
5 |
▶ | if (t->timeout > 0 && !ready_now) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (t->timeout > 0 && !ready_now) { |
1 |
4 |
5 |
| if (t->timeout > 0 && !ready_now) { |
1 |
0 |
1 |
|
| 300 |
|
| SV *argv[2], *timer; |
| 301 |
1 |
| argv[0] = sv_2mortal(newSVnv(t->timeout)); |
| 302 |
1 |
| timer = pmcp_call_method(aTHX_ child, "timer", argv, 1, G_SCALAR, &e); |
| 303 |
1 |
▶ | if (timer) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (timer) { |
1 |
0 |
1 |
|
| 304 |
1 |
| sv_2mortal(timer); |
| 305 |
1 |
| argv[0] = f; |
| 306 |
1 |
| argv[1] = timer; |
| 307 |
1 |
| wait = pmcp_call_method(aTHX_ sv_2mortal(newSVpvs("Punk::Future")), |
| 308 |
|
| "wait_any", argv, 2, G_SCALAR, &e); |
| 309 |
1 |
▶ | if (wait) sv_2mortal(wait); |
Branch: 1 branch
| Branch | True | False | Total |
| if (wait) sv_2mortal(wait); |
1 |
0 |
1 |
|
| 310 |
|
| } |
| 311 |
1 |
▶ | if (e) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e) { |
0 |
1 |
1 |
|
| 312 |
0 |
| SV *msg = pmcp_die_text(aTHX_ e); |
| 313 |
0 |
| SvREFCNT_dec(e); |
| 314 |
0 |
| return pmcp_tool_error(aTHX_ msg, NULL, v); |
| 315 |
|
| } |
| 316 |
|
| } |
| 317 |
5 |
| vals = pmcp_call_method(aTHX_ child, "await", &wait, 1, G_LIST, &e); |
| 318 |
5 |
▶ | if (!vals) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!vals) { |
1 |
4 |
5 |
|
| 319 |
1 |
| SV *msg = pmcp_die_text(aTHX_ e); |
| 320 |
1 |
| SvREFCNT_dec(e); |
| 321 |
1 |
| return pmcp_tool_error(aTHX_ msg, NULL, v); |
| 322 |
|
| } |
| 323 |
4 |
| sv_2mortal(vals); |
| 324 |
4 |
▶ | if (wait != f) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (wait != f) { |
1 |
3 |
4 |
|
| 325 |
1 |
| SV *ready = pmcp_meth0(aTHX_ f, "is_ready"); |
| 326 |
1 |
▶ | int done = ready && SvTRUE(ready); |
Branch: 2 branches
| Branch | True | False | Total |
| int done = ready && SvTRUE(ready); |
1 |
0 |
1 |
| int done = ready && SvTRUE(ready); |
0 |
1 |
1 |
|
| 327 |
1 |
▶ | if (ready) SvREFCNT_dec(ready); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ready) SvREFCNT_dec(ready); |
1 |
0 |
1 |
|
| 328 |
1 |
▶ | if (!done) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!done) { |
1 |
0 |
1 |
|
| 329 |
1 |
| SV *r = pmcp_meth0(aTHX_ f, "cancel"); |
| 330 |
1 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
1 |
0 |
1 |
|
| 331 |
1 |
| return pmcp_tool_error(aTHX_ |
| 332 |
|
| newSVpvf("timed out after %" NVgf " seconds", t->timeout), NULL, v); |
| 333 |
|
| } |
| 334 |
0 |
| vals = pmcp_call_method(aTHX_ f, "get", NULL, 0, G_LIST, &e); |
| 335 |
0 |
▶ | if (!vals) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!vals) { |
0 |
0 |
0 |
|
| 336 |
0 |
| SV *msg = pmcp_die_text(aTHX_ e); |
| 337 |
0 |
| SvREFCNT_dec(e); |
| 338 |
0 |
| return pmcp_tool_error(aTHX_ msg, NULL, v); |
| 339 |
|
| } |
| 340 |
0 |
| sv_2mortal(vals); |
| 341 |
|
| } |
| 342 |
|
| { |
| 343 |
3 |
| SV **first = av_fetch((AV *)SvRV(vals), 0, 0); |
| 344 |
3 |
▶ | val = first ? *first : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| val = first ? *first : &PL_sv_undef; |
3 |
0 |
3 |
|
| 345 |
|
| } |
| 346 |
3 |
| return pmcp_result_of(aTHX_ t, val, v, depth + 1, err); |
| 347 |
|
| } |
| 348 |
|
| |
| 349 |
|
| static SV *pmcp_errors_text(pTHX_ AV *errs, const char *lead); |
| 350 |
|
| |
| 351 |
|
| /* structured: a hash. The result carries it and its JSON as text, and is |
| 352 |
|
| * checked against the output schema. +1 */ |
| 353 |
50 |
| static SV *pmcp_structured_result(pTHX_ pmcp_tool *t, SV *hash, enum pmcp_ver v, |
| 354 |
|
| SV **err) |
| 355 |
|
| { |
| 356 |
|
| HV *r; |
| 357 |
50 |
| SV *text = pmcp_json_trapped(aTHX_ hash, err); |
| 358 |
50 |
▶ | if (!text) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!text) return NULL; |
1 |
49 |
50 |
|
| 359 |
49 |
▶ | if (t->output && v >= PMCP_V_2025_06_18) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (t->output && v >= PMCP_V_2025_06_18) { |
6 |
43 |
49 |
| if (t->output && v >= PMCP_V_2025_06_18) { |
5 |
1 |
6 |
|
| 360 |
5 |
| AV *errs = newAV(); |
| 361 |
5 |
▶ | if (!PMCP_JSF->validate(aTHX_ t->output, hash, errs)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PMCP_JSF->validate(aTHX_ t->output, hash, errs)) { |
2 |
3 |
5 |
|
| 362 |
|
| /* the errors as text: as structuredContent they would not match |
| 363 |
|
| * the schema either */ |
| 364 |
2 |
| SV *msg = pmcp_errors_text(aTHX_ errs, |
| 365 |
|
| "the tool's result does not match its output schema"); |
| 366 |
2 |
| SvREFCNT_dec((SV *)errs); |
| 367 |
2 |
| SvREFCNT_dec(text); |
| 368 |
2 |
| return pmcp_tool_error(aTHX_ msg, NULL, v); |
| 369 |
|
| } |
| 370 |
3 |
| SvREFCNT_dec((SV *)errs); |
| 371 |
|
| } |
| 372 |
47 |
| r = newHV(); |
| 373 |
47 |
| (void)hv_stores(r, "content", pmcp_text_content(aTHX_ text)); |
| 374 |
47 |
▶ | if (v >= PMCP_V_2025_06_18) |
Branch: 1 branch
| Branch | True | False | Total |
| if (v >= PMCP_V_2025_06_18) |
44 |
3 |
47 |
|
| 375 |
44 |
| (void)hv_stores(r, "structuredContent", newSVsv(hash)); |
| 376 |
47 |
| return newRV_noinc((SV *)r); |
| 377 |
|
| } |
| 378 |
|
| |
| 379 |
104 |
| static SV *pmcp_result_of(pTHX_ pmcp_tool *t, SV *val, enum pmcp_ver v, |
| 380 |
|
| int depth, SV **err) |
| 381 |
|
| { |
| 382 |
104 |
▶ | if (depth > 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (depth > 4) { |
0 |
104 |
104 |
|
| 383 |
0 |
| *err = newSVpvs("a result nested too deep"); |
| 384 |
0 |
| return NULL; |
| 385 |
|
| } |
| 386 |
|
| |
| 387 |
|
| /* an output schema promises structured content, so nothing else will do */ |
| 388 |
104 |
▶ | if (t->output && v >= PMCP_V_2025_06_18 && !pmcp_is_hash(val) |
Branch: 3 branches
| Branch | True | False | Total |
| if (t->output && v >= PMCP_V_2025_06_18 && !pmcp_is_hash(val) |
7 |
97 |
104 |
| if (t->output && v >= PMCP_V_2025_06_18 && !pmcp_is_hash(val) |
6 |
1 |
7 |
| if (t->output && v >= PMCP_V_2025_06_18 && !pmcp_is_hash(val) |
1 |
5 |
6 |
|
| 389 |
1 |
▶ | && !pmcp_is_triplet(aTHX_ val)) |
Branch: 1 branch
| Branch | True | False | Total |
| && !pmcp_is_triplet(aTHX_ val)) |
1 |
0 |
1 |
|
| 390 |
1 |
| return pmcp_tool_error(aTHX_ |
| 391 |
|
| newSVpvs("the tool has an output schema and returned no object"), |
| 392 |
|
| NULL, v); |
| 393 |
|
| |
| 394 |
103 |
▶ | if (!SvOK(val)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(val)) { |
1 |
102 |
103 |
|
| 395 |
1 |
| HV *r = newHV(); |
| 396 |
1 |
| (void)hv_stores(r, "content", newRV_noinc((SV *)newAV())); |
| 397 |
1 |
| return newRV_noinc((SV *)r); |
| 398 |
|
| } |
| 399 |
102 |
▶ | if (!SvROK(val) || SvAMAGIC(val)) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (!SvROK(val) || SvAMAGIC(val)) { |
66 |
36 |
102 |
| if (!SvROK(val) || SvAMAGIC(val)) { |
66 |
0 |
66 |
| if (!SvROK(val) || SvAMAGIC(val)) { |
0 |
66 |
66 |
| if (!SvROK(val) || SvAMAGIC(val)) { |
0 |
0 |
0 |
|
| 400 |
36 |
| HV *r = newHV(); |
| 401 |
36 |
| (void)hv_stores(r, "content", pmcp_text_content(aTHX_ pmcp_str(aTHX_ val))); |
| 402 |
36 |
| return newRV_noinc((SV *)r); |
| 403 |
|
| } |
| 404 |
66 |
▶ | if (pmcp_is_triplet(aTHX_ val)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pmcp_is_triplet(aTHX_ val)) { |
23 |
43 |
66 |
|
| 405 |
23 |
| AV *tr = (AV *)SvRV(val); |
| 406 |
23 |
| IV status = SvIV(*av_fetch(tr, 0, 0)); |
| 407 |
|
| int is_json; |
| 408 |
23 |
| SV *body = pmcp_triplet_body(aTHX_ tr, &is_json); |
| 409 |
23 |
| SV *res, *doc = NULL; |
| 410 |
23 |
▶ | if (body && is_json && SvCUR(body)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (body && is_json && SvCUR(body)) { |
23 |
0 |
23 |
| if (body && is_json && SvCUR(body)) { |
19 |
4 |
23 |
| if (body && is_json && SvCUR(body)) { |
19 |
0 |
19 |
|
| 411 |
19 |
| doc = pmcp_rpc_decode(aTHX_ body); |
| 412 |
19 |
▶ | if (doc) sv_2mortal(doc); |
Branch: 1 branch
| Branch | True | False | Total |
| if (doc) sv_2mortal(doc); |
19 |
0 |
19 |
|
| 413 |
|
| } |
| 414 |
23 |
▶ | if (doc && pmcp_is_hash(doc) && status < 400) |
Branch: 3 branches
| Branch | True | False | Total |
| if (doc && pmcp_is_hash(doc) && status < 400) |
19 |
4 |
23 |
| if (doc && pmcp_is_hash(doc) && status < 400) |
16 |
3 |
19 |
| if (doc && pmcp_is_hash(doc) && status < 400) |
11 |
5 |
16 |
|
| 415 |
11 |
| res = pmcp_structured_result(aTHX_ t, doc, v, err); |
| 416 |
|
| else { |
| 417 |
12 |
| HV *r = newHV(); |
| 418 |
12 |
▶ | (void)hv_stores(r, "content", pmcp_text_content(aTHX_ |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_stores(r, "content", pmcp_text_content(aTHX_ |
12 |
0 |
12 |
|
| 419 |
|
| body ? newSVsv(body) : newSVpvs(""))); |
| 420 |
12 |
| res = newRV_noinc((SV *)r); |
| 421 |
|
| } |
| 422 |
23 |
▶ | if (!res) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!res) return NULL; |
0 |
23 |
23 |
|
| 423 |
23 |
▶ | if (status >= 400) |
Branch: 1 branch
| Branch | True | False | Total |
| if (status >= 400) |
6 |
17 |
23 |
|
| 424 |
6 |
| (void)hv_stores((HV *)SvRV(res), "isError", pmcp_bool(aTHX_ 1)); |
| 425 |
|
| { |
| 426 |
23 |
| HV *meta = newHV(); |
| 427 |
23 |
| (void)hv_stores(meta, "status", newSViv(status)); |
| 428 |
23 |
| (void)hv_stores((HV *)SvRV(res), "_meta", newRV_noinc((SV *)meta)); |
| 429 |
|
| } |
| 430 |
23 |
| return res; |
| 431 |
|
| } |
| 432 |
43 |
▶ | if (pmcp_is_hash(val)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pmcp_is_hash(val)) { |
41 |
2 |
43 |
|
| 433 |
41 |
| HV *h = (HV *)SvRV(val); |
| 434 |
|
| /* already a tool result: passed through, isError made a boolean */ |
| 435 |
41 |
▶ | if (hv_exists(h, "content", 7)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_exists(h, "content", 7)) { |
2 |
39 |
41 |
|
| 436 |
2 |
| HV *copy = newHVhv(h); |
| 437 |
2 |
| SV **ie = hv_fetchs(copy, "isError", 0); |
| 438 |
2 |
▶ | if (ie) (void)hv_stores(copy, "isError", pmcp_bool(aTHX_ SvTRUE(*ie))); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ie) (void)hv_stores(copy, "isError", pmcp_bool(aTHX_ SvTRUE(*ie))); |
1 |
1 |
2 |
|
| 439 |
2 |
▶ | if (v < PMCP_V_2025_06_18) (void)hv_delete(copy, "structuredContent", 17, G_DISCARD); |
Branch: 1 branch
| Branch | True | False | Total |
| if (v < PMCP_V_2025_06_18) (void)hv_delete(copy, "structuredContent", 17, G_DISCARD); |
0 |
2 |
2 |
|
| 440 |
2 |
| return newRV_noinc((SV *)copy); |
| 441 |
|
| } |
| 442 |
39 |
| return pmcp_structured_result(aTHX_ t, val, v, err); |
| 443 |
|
| } |
| 444 |
2 |
▶ | if (pmcp_is_array(val)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pmcp_is_array(val)) { |
1 |
1 |
2 |
|
| 445 |
|
| HV *r; |
| 446 |
1 |
| SV *text = pmcp_json_trapped(aTHX_ val, err); |
| 447 |
1 |
▶ | if (!text) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!text) return NULL; |
0 |
1 |
1 |
|
| 448 |
1 |
| r = newHV(); |
| 449 |
1 |
| (void)hv_stores(r, "content", pmcp_text_content(aTHX_ text)); |
| 450 |
1 |
| return newRV_noinc((SV *)r); |
| 451 |
|
| } |
| 452 |
1 |
| *err = newSVpvf("the tool returned a %s reference", sv_reftype(SvRV(val), 0)); |
| 453 |
1 |
| return NULL; |
| 454 |
|
| } |
| 455 |
|
| |
| 456 |
|
| /* ---- tools/call --------------------------------------------------------------- */ |
| 457 |
|
| |
| 458 |
|
| /* The validation errors as one line for the model, after `lead`. +1 */ |
| 459 |
7 |
| static SV *pmcp_errors_text(pTHX_ AV *errs, const char *lead) |
| 460 |
|
| { |
| 461 |
7 |
| SV *out = newSVpvf("%s:", lead); |
| 462 |
7 |
| SSize_t i, n = av_len(errs) + 1; |
| 463 |
15 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
8 |
7 |
15 |
|
| 464 |
8 |
| SV **e = av_fetch(errs, i, 0); |
| 465 |
|
| SV *loc, *msg; |
| 466 |
8 |
▶ | if (!(e && pmcp_is_hash(*e))) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(e && pmcp_is_hash(*e))) continue; |
8 |
0 |
8 |
| if (!(e && pmcp_is_hash(*e))) continue; |
0 |
8 |
8 |
|
| 467 |
8 |
| loc = pmcp_hget(aTHX_ (HV *)SvRV(*e), "instanceLocation"); |
| 468 |
8 |
| msg = pmcp_hget(aTHX_ (HV *)SvRV(*e), "message"); |
| 469 |
22 |
▶ | sv_catpvf(out, "%s %s%s%s", i ? ";" : "", |
Branch: 5 branches
| Branch | True | False | Total |
| sv_catpvf(out, "%s %s%s%s", i ? ";" : "", |
8 |
0 |
8 |
| sv_catpvf(out, "%s %s%s%s", i ? ";" : "", |
8 |
0 |
8 |
| sv_catpvf(out, "%s %s%s%s", i ? ";" : "", |
8 |
0 |
8 |
| sv_catpvf(out, "%s %s%s%s", i ? ";" : "", |
6 |
2 |
8 |
| sv_catpvf(out, "%s %s%s%s", i ? ";" : "", |
1 |
7 |
8 |
|
| 470 |
|
| (loc && SvCUR(loc)) ? SvPV_nolen(loc) : "/", |
| 471 |
|
| msg ? " " : "", msg ? SvPV_nolen(msg) : ""); |
| 472 |
|
| } |
| 473 |
7 |
| return out; |
| 474 |
|
| } |
| 475 |
|
| |
| 476 |
|
| /* The result as bytes, for the dispatcher to splice. Encoded through the |
| 477 |
|
| * trapped encoder, since it may hold anything the action returned. */ |
| 478 |
120 |
| static SV *pmcp_tool_answer(pTHX_ pmcp_req *r, SV *result) |
| 479 |
|
| { |
| 480 |
120 |
| SV *err = NULL; |
| 481 |
120 |
▶ | if (pmcp_is_hash(result)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pmcp_is_hash(result)) { |
120 |
0 |
120 |
|
| 482 |
120 |
| SV **ie = hv_fetchs((HV *)SvRV(result), "isError", 0); |
| 483 |
120 |
▶ | if (ie && SvTRUE(*ie)) r->tool_error = 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (ie && SvTRUE(*ie)) r->tool_error = 1; |
28 |
92 |
120 |
| if (ie && SvTRUE(*ie)) r->tool_error = 1; |
28 |
0 |
28 |
|
| 484 |
|
| } |
| 485 |
120 |
| SV *bytes = pmcp_json_trapped(aTHX_ result, &err); |
| 486 |
120 |
| SvREFCNT_dec(result); |
| 487 |
120 |
▶ | if (!bytes) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bytes) { |
0 |
120 |
120 |
|
| 488 |
0 |
| SvREFCNT_dec(err); |
| 489 |
0 |
| return pmcp_fail(r, PMCP_INTERNAL, "the tool's result is not JSON"); |
| 490 |
|
| } |
| 491 |
120 |
| r->raw = bytes; |
| 492 |
120 |
| return NULL; |
| 493 |
|
| } |
| 494 |
|
| |
| 495 |
134 |
| static SV *pmcp_tools_call(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params) |
| 496 |
|
| { |
| 497 |
134 |
| pmcp_tools *ts = (pmcp_tools *)s->tools; |
| 498 |
134 |
| enum pmcp_ver v = pmcp_req_version(r); |
| 499 |
134 |
▶ | SV **nv = params ? hv_fetchs(params, "name", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **nv = params ? hv_fetchs(params, "name", 0) : NULL; |
134 |
0 |
134 |
|
| 500 |
134 |
▶ | SV **av = params ? hv_fetchs(params, "arguments", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **av = params ? hv_fetchs(params, "arguments", 0) : NULL; |
134 |
0 |
134 |
|
| 501 |
134 |
| SV *args, *call, *child, *why, *ret, *err = NULL, *result; |
| 502 |
|
| pmcp_tool *t; |
| 503 |
|
| |
| 504 |
134 |
▶ | if (!ts) return pmcp_fail(r, PMCP_METHOD, "method not found"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ts) return pmcp_fail(r, PMCP_METHOD, "method not found"); |
0 |
134 |
134 |
|
| 505 |
134 |
▶ | if (!(nv && pmcp_is_str(*nv))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(nv && pmcp_is_str(*nv))) |
133 |
1 |
134 |
| if (!(nv && pmcp_is_str(*nv))) |
1 |
132 |
133 |
|
| 506 |
2 |
| return pmcp_fail(r, PMCP_PARAMS, "a tool name is required"); |
| 507 |
132 |
| t = pmcp_tool_find(aTHX_ ts, *nv); |
| 508 |
132 |
▶ | if (!t) return pmcp_fail(r, PMCP_PARAMS, "unknown tool"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t) return pmcp_fail(r, PMCP_PARAMS, "unknown tool"); |
3 |
129 |
132 |
|
| 509 |
129 |
▶ | if (av && SvOK(*av) && !pmcp_is_hash(*av)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (av && SvOK(*av) && !pmcp_is_hash(*av)) |
53 |
76 |
129 |
| if (av && SvOK(*av) && !pmcp_is_hash(*av)) |
53 |
0 |
53 |
| if (av && SvOK(*av) && !pmcp_is_hash(*av)) |
3 |
50 |
53 |
|
| 510 |
3 |
| return pmcp_fail(r, PMCP_PARAMS, "arguments must be an object"); |
| 511 |
126 |
▶ | if (!r->c) return pmcp_fail(r, PMCP_INTERNAL, "no request to call the tool in"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r->c) return pmcp_fail(r, PMCP_INTERNAL, "no request to call the tool in"); |
0 |
126 |
126 |
|
| 512 |
|
| |
| 513 |
126 |
▶ | args = (av && SvOK(*av)) ? *av : sv_2mortal(newRV_noinc((SV *)newHV())); |
Branch: 2 branches
| Branch | True | False | Total |
| args = (av && SvOK(*av)) ? *av : sv_2mortal(newRV_noinc((SV *)newHV())); |
50 |
76 |
126 |
| args = (av && SvOK(*av)) ? *av : sv_2mortal(newRV_noinc((SV *)newHV())); |
50 |
0 |
50 |
|
| 514 |
126 |
| call = sv_2mortal(pmcp_call_object(aTHX_ "tools/call", t->name, args, v, params)); |
| 515 |
126 |
| pmcp_call_attach(aTHX_ r, call); |
| 516 |
126 |
| child = sv_2mortal(pmcp_child_context(aTHX_ r->c, "tool", t->name, args, call)); |
| 517 |
|
| |
| 518 |
|
| /* the tool's own guard, after the endpoint's and before anything else */ |
| 519 |
126 |
▶ | if ((why = pmcp_run_guards(aTHX_ t->guards, child))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((why = pmcp_run_guards(aTHX_ t->guards, child))) |
5 |
121 |
126 |
|
| 520 |
5 |
| return pmcp_tool_answer(aTHX_ r, pmcp_tool_error(aTHX_ why, NULL, v)); |
| 521 |
|
| |
| 522 |
|
| { |
| 523 |
121 |
| AV *errs = newAV(); |
| 524 |
121 |
▶ | if (!PMCP_JSF->validate(aTHX_ t->input, args, errs)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PMCP_JSF->validate(aTHX_ t->input, args, errs)) { |
9 |
112 |
121 |
|
| 525 |
9 |
| SV *rv = newRV_noinc((SV *)errs); |
| 526 |
|
| /* 2025-11-25 made this a tool error, so the model can fix its |
| 527 |
|
| * arguments and try again. Before it, a protocol error (P5). */ |
| 528 |
9 |
▶ | if (v >= PMCP_V_2025_11_25) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (v >= PMCP_V_2025_11_25) { |
5 |
4 |
9 |
|
| 529 |
5 |
| SV *data = NULL; |
| 530 |
5 |
| SV *text = pmcp_errors_text(aTHX_ errs, "invalid arguments"); |
| 531 |
|
| /* structuredContent must match an output schema, which the |
| 532 |
|
| * errors never do, so such a tool has them as text alone */ |
| 533 |
5 |
▶ | if (t->output) SvREFCNT_dec(rv); |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->output) SvREFCNT_dec(rv); |
1 |
4 |
5 |
|
| 534 |
|
| else { |
| 535 |
4 |
| data = newRV_noinc((SV *)newHV()); |
| 536 |
4 |
| (void)hv_stores((HV *)SvRV(data), "errors", rv); |
| 537 |
|
| } |
| 538 |
5 |
| return pmcp_tool_answer(aTHX_ r, pmcp_tool_error(aTHX_ text, data, v)); |
| 539 |
|
| } |
| 540 |
4 |
| r->edata = newRV_noinc((SV *)newHV()); |
| 541 |
4 |
| (void)hv_stores((HV *)SvRV(r->edata), "errors", rv); |
| 542 |
4 |
| return pmcp_fail(r, PMCP_PARAMS, "invalid arguments"); |
| 543 |
|
| } |
| 544 |
112 |
| SvREFCNT_dec((SV *)errs); |
| 545 |
|
| } |
| 546 |
|
| |
| 547 |
|
| /* an OpenAPI tool is a request the application answers (12) */ |
| 548 |
112 |
▶ | if (t->oa_path) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->oa_path) { |
17 |
95 |
112 |
|
| 549 |
17 |
| SV *why2 = NULL; |
| 550 |
17 |
| SV *val = s->psgi ? pmcp_oa_call(aTHX_ s->psgi, t, r->c, (HV *)SvRV(args), &why2) |
| 551 |
17 |
▶ | : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| : NULL; |
17 |
0 |
17 |
|
| 552 |
17 |
▶ | if (!val) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!val) |
0 |
17 |
17 |
|
| 553 |
0 |
| return pmcp_tool_answer(aTHX_ r, pmcp_tool_error(aTHX_ |
| 554 |
0 |
▶ | why2 ? why2 : newSVpvs("the application is not ready"), NULL, v)); |
Branch: 1 branch
| Branch | True | False | Total |
| why2 ? why2 : newSVpvs("the application is not ready"), NULL, v)); |
0 |
0 |
0 |
|
| 555 |
17 |
| err = NULL; |
| 556 |
17 |
| result = pmcp_result_of(aTHX_ t, val, v, 0, &err); |
| 557 |
17 |
▶ | if (!result) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!result) { |
0 |
17 |
17 |
|
| 558 |
0 |
| SvREFCNT_dec(err); |
| 559 |
0 |
| return pmcp_fail(r, PMCP_INTERNAL, "the tool's result is not JSON"); |
| 560 |
|
| } |
| 561 |
17 |
| return pmcp_tool_answer(aTHX_ r, result); |
| 562 |
|
| } |
| 563 |
|
| |
| 564 |
95 |
| ret = pmcp_call_sv(aTHX_ t->target, &child, 1, G_SCALAR, &err); |
| 565 |
95 |
▶ | if (!ret) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ret) |
6 |
89 |
95 |
|
| 566 |
6 |
| return pmcp_tool_answer(aTHX_ r, |
| 567 |
|
| pmcp_tool_error(aTHX_ pmcp_die_text(aTHX_ sv_2mortal(err)), NULL, v)); |
| 568 |
89 |
| sv_2mortal(ret); |
| 569 |
|
| |
| 570 |
89 |
| err = NULL; |
| 571 |
89 |
▶ | if (SvROK(ret) && sv_derived_from(ret, "Punk::Future")) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(ret) && sv_derived_from(ret, "Punk::Future")) |
52 |
37 |
89 |
| if (SvROK(ret) && sv_derived_from(ret, "Punk::Future")) |
5 |
47 |
52 |
|
| 572 |
5 |
| result = pmcp_await_result(aTHX_ t, child, ret, v, 0, &err); |
| 573 |
|
| else |
| 574 |
84 |
| result = pmcp_result_of(aTHX_ t, ret, v, 0, &err); |
| 575 |
89 |
▶ | if (!result) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!result) { |
2 |
87 |
89 |
|
| 576 |
2 |
| SvREFCNT_dec(err); |
| 577 |
2 |
| return pmcp_fail(r, PMCP_INTERNAL, "the tool's result is not JSON"); |
| 578 |
|
| } |
| 579 |
87 |
| return pmcp_tool_answer(aTHX_ r, result); |
| 580 |
|
| } |
| 581 |
|
| |
| 582 |
|
| /* ---- settling a value, for resources and prompts --------------------------------- */ |
| 583 |
|
| |
| 584 |
|
| /* What an action returned, with a Future awaited within `timeout` seconds. |
| 585 |
|
| * Returns the value (mortal), or NULL with *why set (+1) to a failure's text: |
| 586 |
|
| * a failed Future, a die in the await, or the timeout. The same rules as |
| 587 |
|
| * pmcp_await_result, without a tool result around them. */ |
| 588 |
56 |
| static SV *pmcp_settle(pTHX_ SV *child, SV *ret, NV timeout, SV **why) |
| 589 |
|
| { |
| 590 |
56 |
| SV *wait = ret, *e = NULL, *vals, **first; |
| 591 |
|
| int ready_now; |
| 592 |
|
| |
| 593 |
56 |
| *why = NULL; |
| 594 |
56 |
▶ | if (!(SvROK(ret) && sv_derived_from(ret, "Punk::Future"))) return ret; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(SvROK(ret) && sv_derived_from(ret, "Punk::Future"))) return ret; |
42 |
14 |
56 |
| if (!(SvROK(ret) && sv_derived_from(ret, "Punk::Future"))) return ret; |
40 |
2 |
42 |
|
| 595 |
|
| { |
| 596 |
2 |
| SV *r = pmcp_meth0(aTHX_ ret, "is_ready"); |
| 597 |
2 |
▶ | ready_now = r && SvTRUE(r); |
Branch: 2 branches
| Branch | True | False | Total |
| ready_now = r && SvTRUE(r); |
2 |
0 |
2 |
| ready_now = r && SvTRUE(r); |
2 |
0 |
2 |
|
| 598 |
2 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
2 |
0 |
2 |
|
| 599 |
|
| } |
| 600 |
2 |
▶ | if (timeout > 0 && !ready_now) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (timeout > 0 && !ready_now) { |
0 |
2 |
2 |
| if (timeout > 0 && !ready_now) { |
0 |
0 |
0 |
|
| 601 |
|
| SV *argv[2], *timer; |
| 602 |
0 |
| argv[0] = sv_2mortal(newSVnv(timeout)); |
| 603 |
0 |
| timer = pmcp_call_method(aTHX_ child, "timer", argv, 1, G_SCALAR, &e); |
| 604 |
0 |
▶ | if (timer) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (timer) { |
0 |
0 |
0 |
|
| 605 |
0 |
| sv_2mortal(timer); |
| 606 |
0 |
| argv[0] = ret; |
| 607 |
0 |
| argv[1] = timer; |
| 608 |
0 |
| wait = pmcp_call_method(aTHX_ sv_2mortal(newSVpvs("Punk::Future")), |
| 609 |
|
| "wait_any", argv, 2, G_SCALAR, &e); |
| 610 |
0 |
▶ | if (wait) sv_2mortal(wait); |
Branch: 1 branch
| Branch | True | False | Total |
| if (wait) sv_2mortal(wait); |
0 |
0 |
0 |
|
| 611 |
|
| } |
| 612 |
0 |
▶ | if (e) { *why = pmcp_die_text(aTHX_ e); SvREFCNT_dec(e); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (e) { *why = pmcp_die_text(aTHX_ e); SvREFCNT_dec(e); return NULL; } |
0 |
0 |
0 |
|
| 613 |
|
| } |
| 614 |
2 |
| vals = pmcp_call_method(aTHX_ child, "await", &wait, 1, G_LIST, &e); |
| 615 |
2 |
▶ | if (!vals) { *why = pmcp_die_text(aTHX_ e); SvREFCNT_dec(e); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!vals) { *why = pmcp_die_text(aTHX_ e); SvREFCNT_dec(e); return NULL; } |
0 |
2 |
2 |
|
| 616 |
2 |
| sv_2mortal(vals); |
| 617 |
2 |
▶ | if (wait != ret) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (wait != ret) { |
0 |
2 |
2 |
|
| 618 |
0 |
| SV *ready = pmcp_meth0(aTHX_ ret, "is_ready"); |
| 619 |
0 |
▶ | int done = ready && SvTRUE(ready); |
Branch: 2 branches
| Branch | True | False | Total |
| int done = ready && SvTRUE(ready); |
0 |
0 |
0 |
| int done = ready && SvTRUE(ready); |
0 |
0 |
0 |
|
| 620 |
0 |
▶ | if (ready) SvREFCNT_dec(ready); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ready) SvREFCNT_dec(ready); |
0 |
0 |
0 |
|
| 621 |
0 |
▶ | if (!done) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!done) { |
0 |
0 |
0 |
|
| 622 |
0 |
| SV *r = pmcp_meth0(aTHX_ ret, "cancel"); |
| 623 |
0 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
0 |
0 |
0 |
|
| 624 |
0 |
| *why = newSVpvf("timed out after %" NVgf " seconds", timeout); |
| 625 |
0 |
| return NULL; |
| 626 |
|
| } |
| 627 |
0 |
| vals = pmcp_call_method(aTHX_ ret, "get", NULL, 0, G_LIST, &e); |
| 628 |
0 |
▶ | if (!vals) { *why = pmcp_die_text(aTHX_ e); SvREFCNT_dec(e); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!vals) { *why = pmcp_die_text(aTHX_ e); SvREFCNT_dec(e); return NULL; } |
0 |
0 |
0 |
|
| 629 |
0 |
| sv_2mortal(vals); |
| 630 |
|
| } |
| 631 |
2 |
| first = av_fetch((AV *)SvRV(vals), 0, 0); |
| 632 |
2 |
▶ | return first ? *first : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| return first ? *first : &PL_sv_undef; |
2 |
0 |
2 |
|
| 633 |
|
| } |
| 634 |
|
| |
| 635 |
|
| /* Run a declaration's action over a child context and settle the result. |
| 636 |
|
| * Returns the value (mortal), or NULL with *why (+1) for a die or a failure. */ |
| 637 |
42 |
| static SV *pmcp_run_action(pTHX_ SV *target, SV *child, NV timeout, SV **why) |
| 638 |
|
| { |
| 639 |
42 |
| SV *err = NULL; |
| 640 |
42 |
| SV *ret = pmcp_call_sv(aTHX_ target, &child, 1, G_SCALAR, &err); |
| 641 |
42 |
▶ | if (!ret) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ret) { |
3 |
39 |
42 |
|
| 642 |
3 |
| *why = pmcp_die_text(aTHX_ err); |
| 643 |
3 |
| SvREFCNT_dec(err); |
| 644 |
3 |
| return NULL; |
| 645 |
|
| } |
| 646 |
39 |
| sv_2mortal(ret); |
| 647 |
39 |
| return pmcp_settle(aTHX_ child, ret, timeout, why); |
| 648 |
|
| } |
| 649 |
|
| |
| 650 |
|
| /* ---- context helpers ------------------------------------------------------------ */ |
| 651 |
|
| |
| 652 |
|
| /* A stash key of a context, borrowed, or NULL. Through stash_hv, the raw slot, |
| 653 |
|
| * so asking does not vivify a stash on a request that never had one. */ |
| 654 |
100 |
| static SV *pmcp_stash_get(pTHX_ SV *c, const char *k) |
| 655 |
|
| { |
| 656 |
100 |
| SV *st = pmcp_call_method(aTHX_ c, "stash_hv", NULL, 0, G_SCALAR, NULL); |
| 657 |
100 |
| SV *v = NULL; |
| 658 |
100 |
▶ | if (st && pmcp_is_hash(st)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (st && pmcp_is_hash(st)) { |
100 |
0 |
100 |
| if (st && pmcp_is_hash(st)) { |
99 |
1 |
100 |
|
| 659 |
99 |
| SV **e = hv_fetch((HV *)SvRV(st), k, (I32)strlen(k), 0); |
| 660 |
99 |
▶ | if (e && SvOK(*e)) v = sv_2mortal(newSVsv(*e)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (e && SvOK(*e)) v = sv_2mortal(newSVsv(*e)); |
83 |
16 |
99 |
| if (e && SvOK(*e)) v = sv_2mortal(newSVsv(*e)); |
83 |
0 |
83 |
|
| 661 |
|
| } |
| 662 |
100 |
▶ | if (st) SvREFCNT_dec(st); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st) SvREFCNT_dec(st); |
100 |
0 |
100 |
|
| 663 |
100 |
| return v; |
| 664 |
|
| } |
| 665 |
|
| |
| 666 |
|
| /* $c->mcp: the call object inside a tool call, undef anywhere else. */ |
| 667 |
14 |
| XS_INTERNAL(pmcp_helper_mcp) |
| 668 |
|
| { |
| 669 |
14 |
| dXSARGS; |
| 670 |
14 |
▶ | SV *call = items > 0 ? pmcp_stash_get(aTHX_ ST(0), PMCP_STASH_CALL) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *call = items > 0 ? pmcp_stash_get(aTHX_ ST(0), PMCP_STASH_CALL) : NULL; |
14 |
0 |
14 |
|
| 671 |
14 |
▶ | ST(0) = call ? call : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| ST(0) = call ? call : &PL_sv_undef; |
13 |
1 |
14 |
|
| 672 |
14 |
| XSRETURN(1); |
| 673 |
|
| } |
| 674 |
|
| |
| 675 |
|
| /* $c->mcp_user: what `principal` returned (11), else $c->current_user, else |
| 676 |
|
| * undef. current_user is asked under G_EVAL: an application with no login |
| 677 |
|
| * session may not have one to ask. */ |
| 678 |
15 |
| XS_INTERNAL(pmcp_helper_mcp_user) |
| 679 |
|
| { |
| 680 |
15 |
| dXSARGS; |
| 681 |
15 |
| SV *u = NULL; |
| 682 |
15 |
▶ | if (items > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 0) { |
15 |
0 |
15 |
|
| 683 |
15 |
| SV *c = ST(0); |
| 684 |
15 |
| u = pmcp_stash_get(aTHX_ c, "mcp.user"); |
| 685 |
15 |
▶ | if (!u && pmcp_can(aTHX_ c, "current_user")) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!u && pmcp_can(aTHX_ c, "current_user")) { |
10 |
5 |
15 |
| if (!u && pmcp_can(aTHX_ c, "current_user")) { |
10 |
0 |
10 |
|
| 686 |
10 |
| SV *err = NULL; |
| 687 |
10 |
| SV *r = pmcp_call_method(aTHX_ c, "current_user", NULL, 0, |
| 688 |
|
| G_SCALAR, &err); |
| 689 |
10 |
▶ | if (r) u = sv_2mortal(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) u = sv_2mortal(r); |
0 |
10 |
10 |
|
| 690 |
10 |
| else SvREFCNT_dec(err); |
| 691 |
|
| } |
| 692 |
|
| } |
| 693 |
15 |
| SP = PL_stack_base + ax - 1; |
| 694 |
15 |
▶ | ST(0) = u ? u : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| ST(0) = u ? u : &PL_sv_undef; |
5 |
10 |
15 |
|
| 695 |
15 |
| XSRETURN(1); |
| 696 |
|
| } |
| 697 |
|
| |
| 698 |
|
| #endif /* PMCP_CALL_H */ |