include/pmcp/pmcp_prompt.h

filter: Statementstmt 95.1% 233 / 245 Branchbran 73.5% 178 / 242 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.3% 411 / 487 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pmcp/pmcp_bus.h ↑ Summary include/pmcp/pmcp_clos.h »
1 /* pmcp_prompt.h - prompts: declarations, prompts/list, prompts/get.
2 *
3 * Needs: pmcp_call.h.
4 *
5 * A prompt is an action that returns messages. Its declared arguments are the
6 * names a client may fill, and the required ones are checked before the action
7 * runs. Arguments are strings, as the protocol has them, and reach the action
8 * as params.
9 */
10  
11 #ifndef PMCP_PROMPT_H
12 #define PMCP_PROMPT_H
13  
14 typedef struct {
15 SV *name;
16 SV *target;
17 AV *guards;
18 NV timeout;
19 AV *required; /* names of the required arguments */
20 SV *description; /* or NULL */
21 SV *entry[PMCP_V_LATEST + 1];
22 } pmcp_prompt;
23  
24 typedef struct {
25 pmcp_prompt *p; /* sorted by name */
26 IV n;
27 IV page_size;
28 int any_guard;
29 SV **list[PMCP_V_LATEST + 1];
30 } pmcp_prompts;
31  
32 static const char *const PMCP_PROMPT_KEYS[] = {
33 "title", "description", "arguments", "guard", "icons", "timeout", NULL
34 };
35  
36 3 static void pmcp_prompt_croak(pTHX_ SV *name, const char *what)
37 {
38 3 croak("%s: mcp prompt '%" SVf "' %s", PMCP_WHO, SVfARG(name), what);
39 }
40  
41 /* The arguments as listed: [{name, description?, required}]. +1 AV ref, and
42 * the required names into `req`. */
43 25 static SV *pmcp_prompt_args(pTHX_ SV *name, SV *a, AV *req)
44 {
45 static const char *const keys[] = { "name", "description", "required", NULL };
46 AV *src, *out;
47 SSize_t i, n;
48 25 HV *seen = (HV *)sv_2mortal((SV *)newHV());
49 25 ▶ if (!a) return newRV_noinc((SV *)newAV());
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return newRV_noinc((SV *)newAV()); 17 8 25
50 8 ▶ if (!pmcp_is_array(a))
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_array(a)) 1 7 8
51 1 pmcp_prompt_croak(aTHX_ name, "arguments must be a list of {name, description, required}");
52 7 src = (AV *)SvRV(a);
53 7 out = (AV *)sv_2mortal((SV *)newAV());
54 7 n = av_len(src) + 1;
55 13 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 9 4 13
56 9 SV **e = av_fetch(src, i, 0);
57 HV *h, *o;
58 SV *an, *x;
59 9 ▶ if (!(e && pmcp_is_hash(*e)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(e && pmcp_is_hash(*e))) 9 0 9
if (!(e && pmcp_is_hash(*e))) 0 9 9
60 0 pmcp_prompt_croak(aTHX_ name, "arguments must be a list of {name, description, required}");
61 9 h = (HV *)SvRV(*e);
62 {
63 9 SV *what = sv_2mortal(newSVpvf("mcp prompt '%" SVf "' argument key", SVfARG(name)));
64 9 pmcp_check_keys(aTHX_ SvPV_nolen(what), h, keys);
65 }
66 8 an = pmcp_hget(aTHX_ h, "name");
67 8 ▶ if (!pmcp_is_text(an)) pmcp_prompt_croak(aTHX_ name, "has an argument with no name");
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_text(an)) pmcp_prompt_croak(aTHX_ name, "has an argument with no name"); 1 7 8
68 7 ▶ if (hv_exists_ent(seen, an, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists_ent(seen, an, 0)) { 1 6 7
69 1 SV *m = sv_2mortal(newSVpvf("names the argument '%" SVf "' twice", SVfARG(an)));
70 1 pmcp_prompt_croak(aTHX_ name, SvPV_nolen(m));
71 }
72 6 (void)hv_store_ent(seen, an, newSViv(1), 0);
73 6 o = newHV();
74 6 av_push(out, newRV_noinc((SV *)o));
75 6 (void)hv_stores(o, "name", pmcp_str(aTHX_ an));
76 6 ▶ if ((x = pmcp_hget(aTHX_ h, "description"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((x = pmcp_hget(aTHX_ h, "description"))) { 2 4 6
77 2 ▶ if (!pmcp_is_text(x)) pmcp_prompt_croak(aTHX_ name, "has an argument description that is not a string");
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_text(x)) pmcp_prompt_croak(aTHX_ name, "has an argument description that is not a string"); 0 2 2
78 2 (void)hv_stores(o, "description", pmcp_str(aTHX_ x));
79 }
80 6 x = pmcp_hget(aTHX_ h, "required");
81 6 ▶ (void)hv_stores(o, "required", pmcp_bool(aTHX_ x && SvTRUE(x)));
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(o, "required", pmcp_bool(aTHX_ x && SvTRUE(x))); 4 2 6
(void)hv_stores(o, "required", pmcp_bool(aTHX_ x && SvTRUE(x))); 4 0 4
82 6 ▶ if (x && SvTRUE(x)) av_push(req, newSVsv(an));
Branch: 2 branches
BranchTrueFalseTotal
if (x && SvTRUE(x)) av_push(req, newSVsv(an)); 4 2 6
if (x && SvTRUE(x)) av_push(req, newSVsv(an)); 4 0 4
83 }
84 4 return newRV_inc((SV *)out);
85 }
86  
87 63 static SV *pmcp_prompt_entry(pTHX_ SV *name, HV *spec, SV *args, enum pmcp_ver v)
88 {
89 63 HV *e = newHV();
90 63 SV *x, *bytes, *err = NULL;
91 63 (void)hv_stores(e, "name", newSVsv(name));
92 63 ▶ if ((x = pmcp_hget(aTHX_ spec, "description")))
Branch: 1 branch
BranchTrueFalseTotal
if ((x = pmcp_hget(aTHX_ spec, "description"))) 6 57 63
93 6 (void)hv_stores(e, "description", pmcp_str(aTHX_ x));
94 63 (void)hv_stores(e, "arguments", newSVsv(args));
95 63 ▶ if (v >= PMCP_V_2025_06_18 && (x = pmcp_hget(aTHX_ spec, "title")))
Branch: 2 branches
BranchTrueFalseTotal
if (v >= PMCP_V_2025_06_18 && (x = pmcp_hget(aTHX_ spec, "title"))) 42 21 63
if (v >= PMCP_V_2025_06_18 && (x = pmcp_hget(aTHX_ spec, "title"))) 2 40 42
96 2 (void)hv_stores(e, "title", pmcp_str(aTHX_ x));
97 63 ▶ if (v >= PMCP_V_2025_11_25 && (x = pmcp_hget(aTHX_ spec, "icons")))
Branch: 2 branches
BranchTrueFalseTotal
if (v >= PMCP_V_2025_11_25 && (x = pmcp_hget(aTHX_ spec, "icons"))) 21 42 63
if (v >= PMCP_V_2025_11_25 && (x = pmcp_hget(aTHX_ spec, "icons"))) 1 20 21
98 1 (void)hv_stores(e, "icons", newSVsv(x));
99 63 bytes = pmcp_json_trapped(aTHX_ sv_2mortal(newRV_noinc((SV *)e)), &err);
100 63 ▶ if (!bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (!bytes) { 0 63 63
101 0 sv_2mortal(err);
102 0 croak("%s: mcp prompt '%" SVf "' cannot be listed: %" SVf, PMCP_WHO,
103 SVfARG(name), SVfARG(err));
104 }
105 63 return bytes;
106 }
107  
108 9 static void pmcp_prompts_free(pTHX_ pmcp_prompts *ps)
109 {
110 IV i;
111 int v;
112 9 ▶ if (!ps) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!ps) return; 0 9 9
113 30 ▶ for (i = 0; i < ps->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ps->n; i++) { 21 9 30
114 21 pmcp_prompt *p = &ps->p[i];
115 21 ▶ if (p->name) SvREFCNT_dec(p->name);
Branch: 1 branch
BranchTrueFalseTotal
if (p->name) SvREFCNT_dec(p->name); 21 0 21
116 21 ▶ if (p->target) SvREFCNT_dec(p->target);
Branch: 1 branch
BranchTrueFalseTotal
if (p->target) SvREFCNT_dec(p->target); 21 0 21
117 21 ▶ if (p->guards) SvREFCNT_dec((SV *)p->guards);
Branch: 1 branch
BranchTrueFalseTotal
if (p->guards) SvREFCNT_dec((SV *)p->guards); 1 20 21
118 21 ▶ if (p->required) SvREFCNT_dec((SV *)p->required);
Branch: 1 branch
BranchTrueFalseTotal
if (p->required) SvREFCNT_dec((SV *)p->required); 21 0 21
119 21 ▶ if (p->description) SvREFCNT_dec(p->description);
Branch: 1 branch
BranchTrueFalseTotal
if (p->description) SvREFCNT_dec(p->description); 2 19 21
120 105 ▶ for (v = 0; v <= PMCP_V_LATEST; v++) if (p->entry[v]) SvREFCNT_dec(p->entry[v]);
Branch: 2 branches
BranchTrueFalseTotal
for (v = 0; v <= PMCP_V_LATEST; v++) if (p->entry[v]) SvREFCNT_dec(p->entry[v]); 63 21 84
for (v = 0; v <= PMCP_V_LATEST; v++) if (p->entry[v]) SvREFCNT_dec(p->entry[v]); 84 21 105
121 }
122 45 ▶ for (v = 0; v <= PMCP_V_LATEST; v++) if (ps->list[v]) Safefree(ps->list[v]);
Branch: 2 branches
BranchTrueFalseTotal
for (v = 0; v <= PMCP_V_LATEST; v++) if (ps->list[v]) Safefree(ps->list[v]); 27 9 36
for (v = 0; v <= PMCP_V_LATEST; v++) if (ps->list[v]) Safefree(ps->list[v]); 36 9 45
123 9 Safefree(ps->p);
124 9 Safefree(ps);
125 }
126  
127 36 static int pmcp_prompt_cmp(const void *a, const void *b)
128 {
129 dTHX;
130 36 return sv_cmp(((const pmcp_prompt *)a)->name, ((const pmcp_prompt *)b)->name);
131 }
132  
133 14 static pmcp_prompts *pmcp_prompts_freeze(pTHX_ SV *app, AV *decls, HV *opts)
134 {
135 pmcp_prompts *ps;
136 14 SSize_t i, n = av_len(decls) + 1;
137 SV *v;
138 14 ▶ NV dflt_timeout = (v = pmcp_hget(aTHX_ opts, "timeout")) ? SvNV(v) : 0;
Branch: 1 branch
BranchTrueFalseTotal
NV dflt_timeout = (v = pmcp_hget(aTHX_ opts, "timeout")) ? SvNV(v) : 0; 0 14 14
139  
140 14 Newxz(ps, 1, pmcp_prompts);
141 14 ▶ Newxz(ps->p, n > 0 ? n : 1, pmcp_prompt);
Branch: 2 branches
BranchTrueFalseTotal
Newxz(ps->p, n > 0 ? n : 1, pmcp_prompt); 14 0 14
Newxz(ps->p, n > 0 ? n : 1, pmcp_prompt); 0 14 14
142 14 ▶ ps->page_size = (v = pmcp_hget(aTHX_ opts, "page_size")) ? SvIV(v) : 50;
Branch: 1 branch
BranchTrueFalseTotal
ps->page_size = (v = pmcp_hget(aTHX_ opts, "page_size")) ? SvIV(v) : 50; 14 0 14
143  
144 61 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 52 9 61
145 52 AV *d = (AV *)SvRV(*av_fetch(decls, i, 0));
146 52 SV *name = *av_fetch(d, PMCP_D_KEY, 0);
147 52 HV *spec = (HV *)SvRV(*av_fetch(d, PMCP_D_SPEC, 0));
148 pmcp_prompt *p;
149 SV *args;
150 int ver;
151  
152 104 ▶ if (!strEQ(SvPV_nolen(*av_fetch(d, PMCP_D_KIND, 0)), "prompt")) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!strEQ(SvPV_nolen(*av_fetch(d, PMCP_D_KIND, 0)), "prompt")) continue; 26 26 52
153 {
154 26 SV *what = sv_2mortal(newSVpvf("mcp prompt '%" SVf "' key", SVfARG(name)));
155 26 pmcp_check_keys(aTHX_ SvPV_nolen(what), spec, PMCP_PROMPT_KEYS);
156 }
157 25 ▶ if ((v = pmcp_hget(aTHX_ spec, "title")) && !pmcp_is_text(v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = pmcp_hget(aTHX_ spec, "title")) && !pmcp_is_text(v)) 1 24 25
if ((v = pmcp_hget(aTHX_ spec, "title")) && !pmcp_is_text(v)) 0 1 1
158 0 pmcp_prompt_croak(aTHX_ name, "title must be a string");
159 25 ▶ if ((v = pmcp_hget(aTHX_ spec, "description")) && !pmcp_is_text(v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = pmcp_hget(aTHX_ spec, "description")) && !pmcp_is_text(v)) 2 23 25
if ((v = pmcp_hget(aTHX_ spec, "description")) && !pmcp_is_text(v)) 0 2 2
160 0 pmcp_prompt_croak(aTHX_ name, "description must be a string");
161 25 ▶ if ((v = pmcp_hget(aTHX_ spec, "icons"))) pmcp_check_icons(aTHX_ v);
Branch: 1 branch
BranchTrueFalseTotal
if ((v = pmcp_hget(aTHX_ spec, "icons"))) pmcp_check_icons(aTHX_ v); 1 24 25
162 25 ▶ if ((v = pmcp_hget(aTHX_ spec, "timeout"))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = pmcp_hget(aTHX_ spec, "timeout")) 0 25 25
163 0 ▶ && (SvROK(v) || !looks_like_number(v) || !(SvNV(v) > 0)))
Branch: 3 branches
BranchTrueFalseTotal
&& (SvROK(v) || !looks_like_number(v) || !(SvNV(v) > 0))) 0 0 0
&& (SvROK(v) || !looks_like_number(v) || !(SvNV(v) > 0))) 0 0 0
&& (SvROK(v) || !looks_like_number(v) || !(SvNV(v) > 0))) 0 0 0
164 0 pmcp_prompt_croak(aTHX_ name, "timeout must be a number of seconds above 0");
165  
166 25 p = &ps->p[ps->n++];
167 25 p->name = newSVsv(name);
168 25 p->required = newAV();
169 25 args = sv_2mortal(pmcp_prompt_args(aTHX_ name, pmcp_hget(aTHX_ spec, "arguments"),
170 p->required));
171 21 p->target = newSVsv(*av_fetch(d, PMCP_D_TARGET, 0));
172 22 ▶ p->guards = pmcp_resolve_guards(aTHX_ app, "prompt", name,
Branch: 1 branch
BranchTrueFalseTotal
p->guards = pmcp_resolve_guards(aTHX_ app, "prompt", name, 1 20 21
173 22 hv_exists(spec, "guard", 5) ? *hv_fetchs(spec, "guard", 0) : NULL);
174 21 ▶ if (p->guards) ps->any_guard = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (p->guards) ps->any_guard = 1; 1 20 21
175 21 ▶ p->timeout = (v = pmcp_hget(aTHX_ spec, "timeout")) ? SvNV(v) : dflt_timeout;
Branch: 1 branch
BranchTrueFalseTotal
p->timeout = (v = pmcp_hget(aTHX_ spec, "timeout")) ? SvNV(v) : dflt_timeout; 0 21 21
176 21 ▶ if ((v = pmcp_hget(aTHX_ spec, "description"))) p->description = newSVsv(v);
Branch: 1 branch
BranchTrueFalseTotal
if ((v = pmcp_hget(aTHX_ spec, "description"))) p->description = newSVsv(v); 2 19 21
177 84 ▶ for (ver = PMCP_V_2025_03_26; ver <= PMCP_V_LATEST; ver++)
Branch: 1 branch
BranchTrueFalseTotal
for (ver = PMCP_V_2025_03_26; ver <= PMCP_V_LATEST; ver++) 63 21 84
178 63 p->entry[ver] = pmcp_prompt_entry(aTHX_ name, spec, args, (enum pmcp_ver)ver);
179 }
180 9 ▶ if (ps->n > 1) qsort(ps->p, (size_t)ps->n, sizeof(pmcp_prompt), pmcp_prompt_cmp);
Branch: 1 branch
BranchTrueFalseTotal
if (ps->n > 1) qsort(ps->p, (size_t)ps->n, sizeof(pmcp_prompt), pmcp_prompt_cmp); 1 8 9
181 {
182 int ver;
183 IV k;
184 36 ▶ for (ver = PMCP_V_2025_03_26; ver <= PMCP_V_LATEST; ver++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ver = PMCP_V_2025_03_26; ver <= PMCP_V_LATEST; ver++) { 27 9 36
185 27 ▶ Newxz(ps->list[ver], ps->n > 0 ? ps->n : 1, SV *);
Branch: 2 branches
BranchTrueFalseTotal
Newxz(ps->list[ver], ps->n > 0 ? ps->n : 1, SV *); 27 0 27
Newxz(ps->list[ver], ps->n > 0 ? ps->n : 1, SV *); 0 27 27
186 90 ▶ for (k = 0; k < ps->n; k++) ps->list[ver][k] = ps->p[k].entry[ver];
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < ps->n; k++) ps->list[ver][k] = ps->p[k].entry[ver]; 63 27 90
187 }
188 }
189 9 return ps;
190 }
191  
192 /* ---- prompts/list ------------------------------------------------------------- */
193  
194 typedef struct { pmcp_prompts *ps; SV *c; } pmcp_prompt_list_ctx;
195  
196 26 static int pmcp_prompt_visible(pTHX_ void *vctx, IV i)
197 {
198 26 pmcp_prompt_list_ctx *x = (pmcp_prompt_list_ctx *)vctx;
199 SV *why;
200 26 ▶ if (!x->ps->p[i].guards) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!x->ps->p[i].guards) return 1; 24 2 26
201 2 ▶ if (!x->c) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!x->c) return 0; 0 2 2
202 2 why = pmcp_run_guards(aTHX_ x->ps->p[i].guards, x->c);
203 2 ▶ if (!why) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!why) return 1; 0 2 2
204 2 SvREFCNT_dec(why);
205 2 return 0;
206 }
207  
208 2 static SV *pmcp_prompts_list(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params)
209 {
210 2 pmcp_prompts *ps = (pmcp_prompts *)s->prompts;
211 2 ▶ enum pmcp_ver v = r->version ? r->version : PMCP_V_2025_03_26;
Branch: 1 branch
BranchTrueFalseTotal
enum pmcp_ver v = r->version ? r->version : PMCP_V_2025_03_26; 2 0 2
212 2 ▶ SV **cur = params ? hv_fetchs(params, "cursor", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **cur = params ? hv_fetchs(params, "cursor", 0) : NULL; 2 0 2
213 2 IV start = 0;
214 pmcp_prompt_list_ctx x;
215  
216 2 ▶ if (!ps) return pmcp_fail(r, PMCP_METHOD, "method not found");
Branch: 1 branch
BranchTrueFalseTotal
if (!ps) return pmcp_fail(r, PMCP_METHOD, "method not found"); 0 2 2
217 2 ▶ if (cur && SvOK(*cur)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cur && SvOK(*cur)) { 0 2 2
if (cur && SvOK(*cur)) { 0 0 0
218 0 start = pmcp_cursor_read(aTHX_ *cur, ps->n);
219 0 ▶ if (start < 0) return pmcp_fail(r, PMCP_PARAMS, "invalid cursor");
Branch: 1 branch
BranchTrueFalseTotal
if (start < 0) return pmcp_fail(r, PMCP_PARAMS, "invalid cursor"); 0 0 0
220 }
221 2 x.ps = ps;
222 2 x.c = r->c;
223 2 r->raw = pmcp_page_bytes(aTHX_ "prompts", ps->list[v], ps->n, start,
224 2 ▶ ps->page_size, ps->any_guard ? pmcp_prompt_visible : NULL, &x);
Branch: 1 branch
BranchTrueFalseTotal
ps->page_size, ps->any_guard ? pmcp_prompt_visible : NULL, &x); 2 0 2
225 2 return NULL;
226 }
227  
228 /* ---- prompts/get ----------------------------------------------------------------- */
229  
230 24 static pmcp_prompt *pmcp_prompt_find(pTHX_ pmcp_prompts *ps, SV *name)
231 {
232 24 IV lo = 0, hi = ps->n;
233 74 ▶ while (lo < hi) {
Branch: 1 branch
BranchTrueFalseTotal
while (lo < hi) { 72 2 74
234 72 IV mid = lo + (hi - lo) / 2;
235 72 int c = sv_cmp(name, ps->p[mid].name);
236 72 ▶ if (c == 0) return &ps->p[mid];
Branch: 1 branch
BranchTrueFalseTotal
if (c == 0) return &ps->p[mid]; 22 50 72
237 50 ▶ if (c < 0) hi = mid; else lo = mid + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0) hi = mid; else lo = mid + 1; 27 23 50
238 }
239 2 return NULL;
240 }
241  
242 /* One message: {role, content}, content a string or a content block. Returns
243 * +1, or NULL with *bad set to what was wrong. */
244 12 static SV *pmcp_prompt_message(pTHX_ SV *m, const char **bad)
245 {
246 HV *h, *out;
247 SV *role, *content;
248 const char *rp;
249 12 ▶ if (!pmcp_is_hash(m)) { *bad = "a prompt message is not a {role, content} hash"; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_hash(m)) { *bad = "a prompt message is not a {role, content} hash"; return NULL; } 0 12 12
250 12 h = (HV *)SvRV(m);
251 12 role = pmcp_hget(aTHX_ h, "role");
252 24 ▶ rp = (role && !SvROK(role)) ? SvPV_nolen(role) : "";
Branch: 2 branches
BranchTrueFalseTotal
rp = (role && !SvROK(role)) ? SvPV_nolen(role) : ""; 12 0 12
rp = (role && !SvROK(role)) ? SvPV_nolen(role) : ""; 12 0 12
253 12 ▶ if (!(strEQ(rp, "user") || strEQ(rp, "assistant"))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(strEQ(rp, "user") || strEQ(rp, "assistant"))) { 3 9 12
if (!(strEQ(rp, "user") || strEQ(rp, "assistant"))) { 1 2 3
254 1 *bad = "a prompt message has a role that is not user or assistant";
255 1 return NULL;
256 }
257 11 content = pmcp_hget(aTHX_ h, "content");
258 11 out = newHV();
259 11 (void)hv_stores(out, "role", newSVpv(rp, 0));
260 11 ▶ if (content && pmcp_is_hash(content))
Branch: 2 branches
BranchTrueFalseTotal
if (content && pmcp_is_hash(content)) 10 1 11
if (content && pmcp_is_hash(content)) 1 9 10
261 1 (void)hv_stores(out, "content", newSVsv(content));
262 19 ▶ else if (content && (!SvROK(content) || SvAMAGIC(content))) {
Branch: 5 branches
BranchTrueFalseTotal
else if (content && (!SvROK(content) || SvAMAGIC(content))) { 9 1 10
else if (content && (!SvROK(content) || SvAMAGIC(content))) { 0 9 9
else if (content && (!SvROK(content) || SvAMAGIC(content))) { 0 0 0
else if (content && (!SvROK(content) || SvAMAGIC(content))) { 0 0 0
else if (content && (!SvROK(content) || SvAMAGIC(content))) { 0 0 0
263 9 HV *b = newHV();
264 9 (void)hv_stores(b, "type", newSVpvs("text"));
265 9 (void)hv_stores(b, "text", pmcp_str(aTHX_ content));
266 9 (void)hv_stores(out, "content", newRV_noinc((SV *)b));
267 }
268 else {
269 1 SvREFCNT_dec((SV *)out);
270 1 *bad = "a prompt message has no content";
271 1 return NULL;
272 }
273 10 return newRV_noinc((SV *)out);
274 }
275  
276 25 static SV *pmcp_prompts_get(pTHX_ const pmcp_srv *s, pmcp_req *r, HV *params)
277 {
278 25 pmcp_prompts *ps = (pmcp_prompts *)s->prompts;
279 25 ▶ enum pmcp_ver v = r->version ? r->version : PMCP_V_2025_03_26;
Branch: 1 branch
BranchTrueFalseTotal
enum pmcp_ver v = r->version ? r->version : PMCP_V_2025_03_26; 25 0 25
280 25 ▶ SV **nv = params ? hv_fetchs(params, "name", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **nv = params ? hv_fetchs(params, "name", 0) : NULL; 25 0 25
281 25 ▶ SV **av = params ? hv_fetchs(params, "arguments", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **av = params ? hv_fetchs(params, "arguments", 0) : NULL; 25 0 25
282 25 SV *args, *call, *child, *why = NULL, *val, *desc = NULL, *out, *bytes, *err = NULL;
283 25 AV *msgs = NULL, *list;
284 pmcp_prompt *p;
285 SSize_t i, n;
286  
287 25 ▶ if (!ps) return pmcp_fail(r, PMCP_METHOD, "method not found");
Branch: 1 branch
BranchTrueFalseTotal
if (!ps) return pmcp_fail(r, PMCP_METHOD, "method not found"); 0 25 25
288 25 ▶ if (!(nv && pmcp_is_str(*nv)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(nv && pmcp_is_str(*nv))) 24 1 25
if (!(nv && pmcp_is_str(*nv))) 0 24 24
289 1 return pmcp_fail(r, PMCP_PARAMS, "a prompt name is required");
290 24 p = pmcp_prompt_find(aTHX_ ps, *nv);
291 24 ▶ if (!p) return pmcp_fail(r, PMCP_PARAMS, "unknown prompt");
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return pmcp_fail(r, PMCP_PARAMS, "unknown prompt"); 2 22 24
292 22 ▶ if (av && SvOK(*av) && !pmcp_is_hash(*av))
Branch: 3 branches
BranchTrueFalseTotal
if (av && SvOK(*av) && !pmcp_is_hash(*av)) 20 2 22
if (av && SvOK(*av) && !pmcp_is_hash(*av)) 20 0 20
if (av && SvOK(*av) && !pmcp_is_hash(*av)) 1 19 20
293 1 return pmcp_fail(r, PMCP_PARAMS, "arguments must be an object");
294 21 ▶ if (!r->c) return pmcp_fail(r, PMCP_INTERNAL, "no request to get the prompt in");
Branch: 1 branch
BranchTrueFalseTotal
if (!r->c) return pmcp_fail(r, PMCP_INTERNAL, "no request to get the prompt in"); 0 21 21
295  
296 21 ▶ args = (av && SvOK(*av)) ? *av : sv_2mortal(newRV_noinc((SV *)newHV()));
Branch: 2 branches
BranchTrueFalseTotal
args = (av && SvOK(*av)) ? *av : sv_2mortal(newRV_noinc((SV *)newHV())); 19 2 21
args = (av && SvOK(*av)) ? *av : sv_2mortal(newRV_noinc((SV *)newHV())); 19 0 19
297 21 call = sv_2mortal(pmcp_call_object(aTHX_ "prompts/get", p->name, args, v, params));
298 21 pmcp_call_attach(aTHX_ r, call);
299 21 child = sv_2mortal(pmcp_child_context(aTHX_ r->c, "prompt", p->name, args, call));
300  
301 /* a prompt the caller may not see is one that does not exist */
302 21 ▶ if ((why = pmcp_run_guards(aTHX_ p->guards, child))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((why = pmcp_run_guards(aTHX_ p->guards, child))) { 1 20 21
303 1 SvREFCNT_dec(why);
304 1 return pmcp_fail(r, PMCP_PARAMS, "unknown prompt");
305 }
306  
307 {
308 HE *he;
309 20 HV *ah = (HV *)SvRV(args);
310 20 hv_iterinit(ah);
311 23 ▶ while ((he = hv_iternext(ah))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(ah))) { 5 18 23
312 5 SV *val2 = HeVAL(he);
313 5 ▶ if (SvROK(val2) || !SvOK(val2)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(val2) || !SvOK(val2)) { 4 1 5
if (SvROK(val2) || !SvOK(val2)) { 1 3 4
314 STRLEN kl;
315 2 ▶ const char *k = HePV(he, kl);
Branch: 1 branch
BranchTrueFalseTotal
const char *k = HePV(he, kl); 0 2 2
316 2 r->edata = newRV_noinc((SV *)newHV());
317 2 (void)hv_stores((HV *)SvRV(r->edata), "argument", newSVpvn(k, kl));
318 2 return pmcp_fail(r, PMCP_PARAMS, "prompt arguments must be strings");
319 }
320 }
321 18 n = av_len(p->required) + 1;
322 20 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4 16 20
323 4 SV *rn = *av_fetch(p->required, i, 0);
324 4 ▶ if (!hv_exists_ent(ah, rn, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_exists_ent(ah, rn, 0)) { 2 2 4
325 2 r->edata = newRV_noinc((SV *)newHV());
326 2 (void)hv_stores((HV *)SvRV(r->edata), "argument", newSVsv(rn));
327 2 return pmcp_fail(r, PMCP_PARAMS, "a required argument is missing");
328 }
329 }
330 }
331  
332 16 val = pmcp_run_action(aTHX_ p->target, child, p->timeout, &why);
333 16 ▶ if (!val) {
Branch: 1 branch
BranchTrueFalseTotal
if (!val) { 2 14 16
334 2 r->edata = newRV_noinc((SV *)newHV());
335 2 (void)hv_stores((HV *)SvRV(r->edata), "reason", why);
336 2 return pmcp_fail(r, PMCP_INTERNAL, "the prompt failed");
337 }
338  
339 14 ▶ if (pmcp_is_triplet(aTHX_ val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_is_triplet(aTHX_ val)) { 3 11 14
340 3 AV *tr = (AV *)SvRV(val);
341 3 IV status = SvIV(*av_fetch(tr, 0, 0));
342 int is_json;
343 3 SV *body = pmcp_triplet_body(aTHX_ tr, &is_json);
344 3 ▶ if (status >= 400 || !body) {
Branch: 2 branches
BranchTrueFalseTotal
if (status >= 400 || !body) { 1 2 3
if (status >= 400 || !body) { 0 1 1
345 2 r->edata = newRV_noinc((SV *)newHV());
346 2 (void)hv_stores((HV *)SvRV(r->edata), "status", newSViv(status));
347 2 return pmcp_fail(r, PMCP_INTERNAL, "the prompt failed");
348 }
349 1 val = body;
350 }
351  
352 12 list = (AV *)sv_2mortal((SV *)newAV());
353 12 ▶ if (!SvOK(val)) return pmcp_fail(r, PMCP_INTERNAL, "the prompt returned nothing");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(val)) return pmcp_fail(r, PMCP_INTERNAL, "the prompt returned nothing"); 1 11 12
354 16 ▶ if (!SvROK(val) || SvAMAGIC(val)) {
Branch: 4 branches
BranchTrueFalseTotal
if (!SvROK(val) || SvAMAGIC(val)) { 6 5 11
if (!SvROK(val) || SvAMAGIC(val)) { 6 0 6
if (!SvROK(val) || SvAMAGIC(val)) { 0 6 6
if (!SvROK(val) || SvAMAGIC(val)) { 0 0 0
355 5 HV *m = newHV();
356 5 (void)hv_stores(m, "role", newSVpvs("user"));
357 5 (void)hv_stores(m, "content", newSVsv(val));
358 5 av_push(list, newRV_noinc((SV *)m));
359 }
360 6 ▶ else if (pmcp_is_hash(val) && pmcp_is_array(pmcp_hget(aTHX_ (HV *)SvRV(val), "messages"))) {
Branch: 2 branches
BranchTrueFalseTotal
else if (pmcp_is_hash(val) && pmcp_is_array(pmcp_hget(aTHX_ (HV *)SvRV(val), "messages"))) { 2 4 6
else if (pmcp_is_hash(val) && pmcp_is_array(pmcp_hget(aTHX_ (HV *)SvRV(val), "messages"))) { 1 1 2
361 1 msgs = (AV *)SvRV(pmcp_hget(aTHX_ (HV *)SvRV(val), "messages"));
362 1 desc = pmcp_hget(aTHX_ (HV *)SvRV(val), "description");
363 }
364 5 ▶ else if (pmcp_is_array(val)) msgs = (AV *)SvRV(val);
Branch: 1 branch
BranchTrueFalseTotal
else if (pmcp_is_array(val)) msgs = (AV *)SvRV(val); 4 1 5
365 1 ▶ else if (pmcp_is_hash(val)) av_push(list, newSVsv(val));
Branch: 1 branch
BranchTrueFalseTotal
else if (pmcp_is_hash(val)) av_push(list, newSVsv(val)); 1 0 1
366 0 else return pmcp_fail(r, PMCP_INTERNAL, "the prompt returned something that is not messages");
367 11 ▶ if (msgs) {
Branch: 1 branch
BranchTrueFalseTotal
if (msgs) { 5 6 11
368 5 n = av_len(msgs) + 1;
369 11 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 6 5 11
370 6 SV **e = av_fetch(msgs, i, 0);
371 6 ▶ av_push(list, newSVsv(e ? *e : &PL_sv_undef));
Branch: 1 branch
BranchTrueFalseTotal
av_push(list, newSVsv(e ? *e : &PL_sv_undef)); 6 0 6
372 }
373 }
374  
375 {
376 11 AV *clean = newAV();
377 11 n = av_len(list) + 1;
378 21 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 12 9 21
379 12 const char *bad = NULL;
380 12 SV *m = pmcp_prompt_message(aTHX_ *av_fetch(list, i, 0), &bad);
381 12 ▶ if (!m) {
Branch: 1 branch
BranchTrueFalseTotal
if (!m) { 2 10 12
382 2 SvREFCNT_dec((SV *)clean);
383 2 return pmcp_fail(r, PMCP_INTERNAL, bad);
384 }
385 10 av_push(clean, m);
386 }
387 9 out = newRV_noinc((SV *)newHV());
388 9 ▶ if (desc) (void)hv_stores((HV *)SvRV(out), "description", pmcp_str(aTHX_ desc));
Branch: 1 branch
BranchTrueFalseTotal
if (desc) (void)hv_stores((HV *)SvRV(out), "description", pmcp_str(aTHX_ desc)); 1 8 9
389 8 ▶ else if (p->description)
Branch: 1 branch
BranchTrueFalseTotal
else if (p->description) 2 6 8
390 2 (void)hv_stores((HV *)SvRV(out), "description", newSVsv(p->description));
391 9 (void)hv_stores((HV *)SvRV(out), "messages", newRV_noinc((SV *)clean));
392 }
393 9 bytes = pmcp_json_trapped(aTHX_ out, &err);
394 9 SvREFCNT_dec(out);
395 9 ▶ if (!bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (!bytes) { 0 9 9
396 0 SvREFCNT_dec(err);
397 0 return pmcp_fail(r, PMCP_INTERNAL, "the prompt is not JSON");
398 }
399 9 r->raw = bytes;
400 9 return NULL;
401 }
402  
403 #endif /* PMCP_PROMPT_H */
« include/pmcp/pmcp_bus.h ↑ Summary include/pmcp/pmcp_clos.h »