@try-works/dsh-righthand 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/lib/index.d.ts +3 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +4 -1
- package/lib/index.js.map +1 -1
- package/lib/text-tools.d.ts +18 -0
- package/lib/text-tools.d.ts.map +1 -0
- package/lib/text-tools.js +158 -0
- package/lib/text-tools.js.map +1 -0
- package/package.json +1 -1
- package/skills/dsh-righthand/SKILL.md +9 -1
- package/src/index.ts +4 -1
- package/src/text-tools.ts +173 -0
package/README.md
CHANGED
|
@@ -26,6 +26,7 @@ dsh --profile <profile> --patch ./cordis.patch.yml
|
|
|
26
26
|
|---|---|---|
|
|
27
27
|
| `rh_store_put` / `rh_store_get` / `rh_store_delete` / `rh_store_list` | store | `ctx.storageDomain` (domain KV) |
|
|
28
28
|
| `rh_task_create` / `rh_task_list` / `rh_task_next` / `rh_task_update` / `rh_task_delete` | tasks | `ctx.storageDomain` (typed `righthand_tasks` domain; state machine open → done/failed) |
|
|
29
|
+
| `rh_text_summarise` / `rh_text_extract` / `rh_text_classify` / `rh_text_translate` | text | `ctx.llm` (one model call per verb) |
|
|
29
30
|
| `rh_credential_describe` / `rh_credential_set` / `rh_credential_unset` | secrets | `ctx.credentials` (values never echoed) |
|
|
30
31
|
| `rh_settings_get` / `rh_settings_set` | secrets | `ctx.settings` (namespace `righthand`) |
|
|
31
32
|
| `rh_run` | exec | `ctx.subprocess` (collect mode, bounded output) |
|
package/lib/index.d.ts
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
* - store-tools: rh_store_put/get/delete/list over ctx.storageDomain
|
|
16
16
|
* - secrets-tools: rh_credential_describe/set/unset + rh_settings_get/set
|
|
17
17
|
* - task-tools: rh_task_create/list/next/update/delete over ctx.storageDomain
|
|
18
|
+
* - text-tools: rh_text_summarise/extract/classify/translate over ctx.llm
|
|
18
19
|
* - exec-tools: rh_run / rh_run_bg over ctx.subprocess + ctx.jobs
|
|
19
20
|
* - guard-tools: tools/pre-execute policy (config.rules)
|
|
20
21
|
* - skills: the packaged dsh-righthand skill (ctx.skills)
|
|
@@ -27,6 +28,7 @@ import * as secretsTools from './secrets-tools.ts';
|
|
|
27
28
|
import * as execTools from './exec-tools.ts';
|
|
28
29
|
import * as guardTools from './guard-tools.ts';
|
|
29
30
|
import * as taskTools from './task-tools.ts';
|
|
31
|
+
import * as textTools from './text-tools.ts';
|
|
30
32
|
import * as righthandSkills from './skills.ts';
|
|
31
33
|
import type { GuardRule } from './guard-tools.ts';
|
|
32
34
|
export declare const name = "dsh-righthand";
|
|
@@ -44,7 +46,7 @@ export interface RighthandConfig {
|
|
|
44
46
|
* instead of failing the whole plugin.
|
|
45
47
|
*/
|
|
46
48
|
export declare function apply(ctx: Context, config?: RighthandConfig): void;
|
|
47
|
-
export { storeTools, secretsTools, execTools, taskTools, guardTools, righthandSkills };
|
|
49
|
+
export { storeTools, secretsTools, execTools, taskTools, textTools, guardTools, righthandSkills };
|
|
48
50
|
export { SKILL_NAME, SKILL_DESCRIPTION, skillDirectory, skillBody } from './skills.ts';
|
|
49
51
|
export type { GuardRule } from './guard-tools.ts';
|
|
50
52
|
export { guardFactsFor } from './guard-tools.ts';
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAClD,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAA;AAC9C,OAAO,KAAK,YAAY,MAAM,oBAAoB,CAAA;AAClD,OAAO,KAAK,SAAS,MAAM,iBAAiB,CAAA;AAC5C,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAA;AAC9C,OAAO,KAAK,SAAS,MAAM,iBAAiB,CAAA;AAC5C,OAAO,KAAK,SAAS,MAAM,iBAAiB,CAAA;AAC5C,OAAO,KAAK,eAAe,MAAM,aAAa,CAAA;AAC9C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAEjD,eAAO,MAAM,IAAI,kBAAkB,CAAA;AAEnC,iFAAiF;AACjF,eAAO,MAAM,MAAM,EAAE,MAAM,EAAO,CAAA;AAElC,qCAAqC;AACrC,MAAM,WAAW,eAAe;IAC9B,qEAAqE;IACrE,KAAK,CAAC,EAAE,SAAS,EAAE,CAAA;CACpB;AAED;;;;;GAKG;AACH,wBAAgB,KAAK,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,GAAE,eAAoB,GAAG,IAAI,CAQtE;AAGD,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,eAAe,EAAE,CAAA;AACjG,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACtF,YAAY,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAChD,YAAY,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA"}
|
package/lib/index.js
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
* - store-tools: rh_store_put/get/delete/list over ctx.storageDomain
|
|
16
16
|
* - secrets-tools: rh_credential_describe/set/unset + rh_settings_get/set
|
|
17
17
|
* - task-tools: rh_task_create/list/next/update/delete over ctx.storageDomain
|
|
18
|
+
* - text-tools: rh_text_summarise/extract/classify/translate over ctx.llm
|
|
18
19
|
* - exec-tools: rh_run / rh_run_bg over ctx.subprocess + ctx.jobs
|
|
19
20
|
* - guard-tools: tools/pre-execute policy (config.rules)
|
|
20
21
|
* - skills: the packaged dsh-righthand skill (ctx.skills)
|
|
@@ -26,6 +27,7 @@ import * as secretsTools from "./secrets-tools.js";
|
|
|
26
27
|
import * as execTools from "./exec-tools.js";
|
|
27
28
|
import * as guardTools from "./guard-tools.js";
|
|
28
29
|
import * as taskTools from "./task-tools.js";
|
|
30
|
+
import * as textTools from "./text-tools.js";
|
|
29
31
|
import * as righthandSkills from "./skills.js";
|
|
30
32
|
export const name = 'dsh-righthand';
|
|
31
33
|
/** The combined plugin needs no services itself; each child waits on its own. */
|
|
@@ -41,11 +43,12 @@ export function apply(ctx, config = {}) {
|
|
|
41
43
|
ctx.plugin(secretsTools);
|
|
42
44
|
ctx.plugin(execTools);
|
|
43
45
|
ctx.plugin(taskTools);
|
|
46
|
+
ctx.plugin(textTools);
|
|
44
47
|
ctx.plugin(guardTools, { rules: config.rules ?? [] });
|
|
45
48
|
ctx.plugin(righthandSkills);
|
|
46
49
|
}
|
|
47
50
|
// Individual modules stay importable for selective mounting.
|
|
48
|
-
export { storeTools, secretsTools, execTools, taskTools, guardTools, righthandSkills };
|
|
51
|
+
export { storeTools, secretsTools, execTools, taskTools, textTools, guardTools, righthandSkills };
|
|
49
52
|
export { SKILL_NAME, SKILL_DESCRIPTION, skillDirectory, skillBody } from "./skills.js";
|
|
50
53
|
export { guardFactsFor } from "./guard-tools.js";
|
|
51
54
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAGH,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAA;AAC9C,OAAO,KAAK,YAAY,MAAM,oBAAoB,CAAA;AAClD,OAAO,KAAK,SAAS,MAAM,iBAAiB,CAAA;AAC5C,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAA;AAC9C,OAAO,KAAK,SAAS,MAAM,iBAAiB,CAAA;AAC5C,OAAO,KAAK,SAAS,MAAM,iBAAiB,CAAA;AAC5C,OAAO,KAAK,eAAe,MAAM,aAAa,CAAA;AAG9C,MAAM,CAAC,MAAM,IAAI,GAAG,eAAe,CAAA;AAEnC,iFAAiF;AACjF,MAAM,CAAC,MAAM,MAAM,GAAa,EAAE,CAAA;AAQlC;;;;;GAKG;AACH,MAAM,UAAU,KAAK,CAAC,GAAY,EAAE,SAA0B,EAAE;IAC9D,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,CAAA;IACtB,GAAG,CAAC,MAAM,CAAC,YAAY,CAAC,CAAA;IACxB,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;IACrB,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;IACrB,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;IACrB,GAAG,CAAC,MAAM,CAAC,UAAU,EAAE,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,IAAI,EAAE,EAAE,CAAC,CAAA;IACrD,GAAG,CAAC,MAAM,CAAC,eAAe,CAAC,CAAA;AAC7B,CAAC;AAED,6DAA6D;AAC7D,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,eAAe,EAAE,CAAA;AACjG,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAEtF,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* text-tools — rh_text_* over ctx.llm.
|
|
3
|
+
* Language work at one call per verb: summarise, extract-to-JSON,
|
|
4
|
+
* classify, translate. Same prompt discipline as the digest summarizer:
|
|
5
|
+
* roles stay roles, caps are deliberate, each call's failure is contained
|
|
6
|
+
* in that call (the other tools are unaffected).
|
|
7
|
+
* @module dsh-righthand/text-tools
|
|
8
|
+
*/
|
|
9
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
10
|
+
export declare const name = "righthand-text";
|
|
11
|
+
export declare const inject: string[];
|
|
12
|
+
export interface TextConfig {
|
|
13
|
+
/** LLM route; defaults match the harness default model. */
|
|
14
|
+
provider?: string;
|
|
15
|
+
model?: string;
|
|
16
|
+
}
|
|
17
|
+
export declare function apply(ctx: Context, config?: TextConfig): void;
|
|
18
|
+
//# sourceMappingURL=text-tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text-tools.d.ts","sourceRoot":"","sources":["../src/text-tools.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAKlD,eAAO,MAAM,IAAI,mBAAmB,CAAA;AACpC,eAAO,MAAM,MAAM,UAAmB,CAAA;AAEtC,MAAM,WAAW,UAAU;IACzB,2DAA2D;IAC3D,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AA8CD,wBAAgB,KAAK,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,GAAE,UAAe,GAAG,IAAI,CAoGjE"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* text-tools — rh_text_* over ctx.llm.
|
|
3
|
+
* Language work at one call per verb: summarise, extract-to-JSON,
|
|
4
|
+
* classify, translate. Same prompt discipline as the digest summarizer:
|
|
5
|
+
* roles stay roles, caps are deliberate, each call's failure is contained
|
|
6
|
+
* in that call (the other tools are unaffected).
|
|
7
|
+
* @module dsh-righthand/text-tools
|
|
8
|
+
*/
|
|
9
|
+
import { defineTool } from '@deepseek-ai/dsh-tools';
|
|
10
|
+
import { BlockAssembler, createUserMessage, deepFreeze } from '@deepseek-ai/dsh-llm';
|
|
11
|
+
export const name = 'righthand-text';
|
|
12
|
+
export const inject = ['tools', 'llm'];
|
|
13
|
+
const DEFAULTS = { provider: 'deepseek-official', model: 'deepseek-v4-flash' };
|
|
14
|
+
/** One LLM call, text in, text out. Returns the assembled text or throws. */
|
|
15
|
+
async function ask(ctx, cfg, system, user, maxTokens, signal) {
|
|
16
|
+
const options = deepFreeze({
|
|
17
|
+
provider: cfg.provider,
|
|
18
|
+
model: cfg.model,
|
|
19
|
+
messages: [createUserMessage({
|
|
20
|
+
content: [{ type: 'text', text: user }],
|
|
21
|
+
source: { kind: 'plugin', plugin: 'righthand-text' },
|
|
22
|
+
})],
|
|
23
|
+
system,
|
|
24
|
+
maxTokens,
|
|
25
|
+
purpose: 'compaction',
|
|
26
|
+
signal,
|
|
27
|
+
});
|
|
28
|
+
const assembler = new BlockAssembler();
|
|
29
|
+
for await (const chunk of ctx.llm.stream(options))
|
|
30
|
+
assembler.push(chunk);
|
|
31
|
+
if (assembler.finish.kind !== 'stop')
|
|
32
|
+
throw new Error('text model call did not stop cleanly: ' + JSON.stringify(assembler.finish.kind));
|
|
33
|
+
const text = assembler.blocks().filter((b) => b.type === 'text').map((b) => b.text).join(' ').trim();
|
|
34
|
+
if (text === '')
|
|
35
|
+
throw new Error('text model call produced no text');
|
|
36
|
+
return text;
|
|
37
|
+
}
|
|
38
|
+
/** Extract the outermost {...} JSON object from a model reply. */
|
|
39
|
+
function parseJsonWindow(text) {
|
|
40
|
+
const start = text.indexOf('{');
|
|
41
|
+
const end = text.lastIndexOf('}');
|
|
42
|
+
if (start < 0 || end <= start)
|
|
43
|
+
throw new Error('model output was not JSON');
|
|
44
|
+
return JSON.parse(text.slice(start, end + 1));
|
|
45
|
+
}
|
|
46
|
+
/** Summarise: plain prose, no JSON. */
|
|
47
|
+
const SUMMARISE_SYSTEM = 'You summarise text. Return ONLY the summary itself - a few sentences, plain prose, no JSON, no Markdown headers, no quotes around it. Preserve the language of the input.';
|
|
48
|
+
/** Extract: JSON matching a caller schema. */
|
|
49
|
+
const EXTRACT_SYSTEM = 'You extract structured data from text. Return ONLY one JSON object matching the caller schema exactly - no prose, no Markdown, no code fences.';
|
|
50
|
+
/** Classify: one label plus confidence, as JSON. */
|
|
51
|
+
const CLASSIFY_SYSTEM = 'You classify text. Return ONLY one JSON object with exactly two keys: label (one of the given labels, verbatim) and confidence (a number from 0 to 1). No prose, no Markdown.';
|
|
52
|
+
/** Translate: preserve formatting, plain prose. */
|
|
53
|
+
const TRANSLATE_SYSTEM = 'You translate text. Return ONLY the translation - plain prose, no JSON, no quotes around it, preserving paragraph breaks and any Markdown or code the input contains.';
|
|
54
|
+
export function apply(ctx, config = {}) {
|
|
55
|
+
const cfg = { ...DEFAULTS, ...config };
|
|
56
|
+
ctx.tools.register(defineTool({
|
|
57
|
+
name: 'rh_text_summarise',
|
|
58
|
+
description: 'Summarise text into a few sentences. One model call per invocation; the result is new prose, not extracted sentences.',
|
|
59
|
+
parameters: {
|
|
60
|
+
text: { type: 'string', required: true, description: 'The text to summarise (max ~30000 characters).' },
|
|
61
|
+
sentences: { type: 'integer', description: 'Target length in sentences (default 3).' },
|
|
62
|
+
},
|
|
63
|
+
output: {
|
|
64
|
+
schema: {
|
|
65
|
+
type: 'object',
|
|
66
|
+
additionalProperties: false,
|
|
67
|
+
properties: {
|
|
68
|
+
summary: { type: 'string', required: true },
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
render: (_args, value) => [{ type: 'text', text: value.summary }],
|
|
72
|
+
},
|
|
73
|
+
async execute(args, exec) {
|
|
74
|
+
const n = args.sentences ?? 3;
|
|
75
|
+
const summary = await ask(ctx, cfg, SUMMARISE_SYSTEM, 'Summarise this in about ' + n + ' sentences:\n\n' + args.text, 600, exec.signal);
|
|
76
|
+
return { summary };
|
|
77
|
+
},
|
|
78
|
+
}));
|
|
79
|
+
ctx.tools.register(defineTool({
|
|
80
|
+
name: 'rh_text_extract',
|
|
81
|
+
description: 'Turn text into JSON matching a schema you give. The schema is a JSON Schema object; the model returns one object conforming to it, or the call fails cleanly.',
|
|
82
|
+
parameters: {
|
|
83
|
+
text: { type: 'string', required: true, description: 'The text to extract from.' },
|
|
84
|
+
schema: { type: 'json', required: true, description: 'JSON Schema the result must match, e.g. { "type": "object", "properties": { "name": { "type": "string" } }, "required": ["name"] }.' },
|
|
85
|
+
},
|
|
86
|
+
output: {
|
|
87
|
+
schema: {
|
|
88
|
+
type: 'object',
|
|
89
|
+
additionalProperties: false,
|
|
90
|
+
properties: {
|
|
91
|
+
extracted: { type: 'json', required: true },
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
render: (_args, value) => [{ type: 'text', text: JSON.stringify(value.extracted) }],
|
|
95
|
+
},
|
|
96
|
+
async execute(args, exec) {
|
|
97
|
+
const framed = 'Schema:\n' + JSON.stringify(args.schema) + '\n\nText:\n' + args.text;
|
|
98
|
+
const raw = await ask(ctx, cfg, EXTRACT_SYSTEM, framed, 800, exec.signal);
|
|
99
|
+
return { extracted: parseJsonWindow(raw) };
|
|
100
|
+
},
|
|
101
|
+
}));
|
|
102
|
+
ctx.tools.register(defineTool({
|
|
103
|
+
name: 'rh_text_classify',
|
|
104
|
+
description: 'Sort text into one of the labels you give, with a confidence score from 0 to 1. The label returned is one of yours, verbatim.',
|
|
105
|
+
parameters: {
|
|
106
|
+
text: { type: 'string', required: true, description: 'The text to classify.' },
|
|
107
|
+
labels: { type: 'array', required: true, items: { type: 'string' }, description: 'The candidate labels, e.g. ["bug", "feature request", "question"].' },
|
|
108
|
+
},
|
|
109
|
+
output: {
|
|
110
|
+
schema: {
|
|
111
|
+
type: 'object',
|
|
112
|
+
additionalProperties: false,
|
|
113
|
+
properties: {
|
|
114
|
+
label: { type: 'string', required: true },
|
|
115
|
+
confidence: { type: 'number', required: true },
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
render: (_args, value) => [{ type: 'text', text: value.label + ' (' + value.confidence + ')' }],
|
|
119
|
+
},
|
|
120
|
+
async execute(args, exec) {
|
|
121
|
+
if (args.labels.length === 0)
|
|
122
|
+
throw new Error('classify needs at least one label');
|
|
123
|
+
const framed = 'Labels: ' + JSON.stringify(args.labels) + '\n\nText:\n' + args.text;
|
|
124
|
+
const raw = await ask(ctx, cfg, CLASSIFY_SYSTEM, framed, 100, exec.signal);
|
|
125
|
+
const parsed = parseJsonWindow(raw);
|
|
126
|
+
if (typeof parsed.label !== 'string' || !args.labels.includes(parsed.label))
|
|
127
|
+
throw new Error('model returned a label outside the given set');
|
|
128
|
+
const confidence = Number(parsed.confidence);
|
|
129
|
+
if (!Number.isFinite(confidence) || confidence < 0 || confidence > 1)
|
|
130
|
+
throw new Error('model returned an invalid confidence');
|
|
131
|
+
return { label: parsed.label, confidence };
|
|
132
|
+
},
|
|
133
|
+
}));
|
|
134
|
+
ctx.tools.register(defineTool({
|
|
135
|
+
name: 'rh_text_translate',
|
|
136
|
+
description: 'Translate text into another language, preserving formatting (paragraph breaks, Markdown, code).',
|
|
137
|
+
parameters: {
|
|
138
|
+
text: { type: 'string', required: true, description: 'The text to translate.' },
|
|
139
|
+
language: { type: 'string', required: true, description: 'The target language, e.g. "Spanish", "zh-CN", "Japanese".' },
|
|
140
|
+
},
|
|
141
|
+
output: {
|
|
142
|
+
schema: {
|
|
143
|
+
type: 'object',
|
|
144
|
+
additionalProperties: false,
|
|
145
|
+
properties: {
|
|
146
|
+
translation: { type: 'string', required: true },
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
render: (_args, value) => [{ type: 'text', text: value.translation }],
|
|
150
|
+
},
|
|
151
|
+
async execute(args, exec) {
|
|
152
|
+
const framed = 'Translate the following into ' + args.language + ':\n\n' + args.text;
|
|
153
|
+
const translation = await ask(ctx, cfg, TRANSLATE_SYSTEM, framed, 800, exec.signal);
|
|
154
|
+
return { translation };
|
|
155
|
+
},
|
|
156
|
+
}));
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=text-tools.js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AE,WAAW,EAAE,CAAA;QACxB,CAAC;KACF,CAAC,CAAC,CAAA;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@try-works/dsh-righthand",
|
|
3
3
|
"description": "DeepSeek Harness plugin: DSH-native righthand tools — durable KV store, credential/settings management, governed exec, and a tool guard",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.5",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
7
7
|
"types": "lib/index.d.ts",
|
|
@@ -16,6 +16,7 @@ The righthand toolkit: DSH-native tools over the harness's own services. Use thi
|
|
|
16
16
|
| secrets | `rh_credential_describe` / `rh_credential_set` / `rh_credential_unset` | `ctx.credentials` | The task must check, store, or remove a credential reference (e.g. `CLOUDFLARE_API_TOKEN`). Values are written durably and **never echoed back**. |
|
|
17
17
|
| settings | `rh_settings_get` / `rh_settings_set` | `ctx.settings` (namespace `righthand`) | Reading or patching righthand settings (`accountId`, `defaultZone`, `defaultScriptPrefix`). |
|
|
18
18
|
| exec | `rh_run` / `rh_run_bg` | `ctx.subprocess` + `ctx.jobs` | Running one command (argv array, no shell interpretation). `rh_run` collects bounded output; `rh_run_bg` starts an owner-scoped background job and returns its id. |
|
|
19
|
+
| text | `rh_text_summarise` / `rh_text_extract` / `rh_text_classify` / `rh_text_translate` | `ctx.llm` (one model call per verb) | Language work: shorten, turn into JSON matching a schema, sort into your labels with confidence, or translate preserving formatting. |
|
|
19
20
|
| guard | (policy, not a tool) | `ctx.tools` `tools/pre-execute` | Configured via plugin config `rules`; gates tools by name prefix with `allow` / `deny` / `ask` modes. |
|
|
20
21
|
|
|
21
22
|
## Store semantics
|
|
@@ -24,6 +25,13 @@ The righthand toolkit: DSH-native tools over the harness's own services. Use thi
|
|
|
24
25
|
- `rh_store_get` returns `{ found, key, value?, updatedAt? }` — `found: false` means the key is absent, not an error.
|
|
25
26
|
- Writes are durable (backend flush before commit) and serialized on one write chain; values must be JSON-serializable.
|
|
26
27
|
|
|
28
|
+
## Text semantics
|
|
29
|
+
|
|
30
|
+
- One model call per verb; each call's failure is contained (the other tools are unaffected).
|
|
31
|
+
- `rh_text_extract` takes a JSON Schema and the model returns one object conforming to it — parse failures surface as a clean error, not raw prose.
|
|
32
|
+
- `rh_text_classify` returns a label verbatim from the given set plus a 0–1 confidence; a label outside the set is an error.
|
|
33
|
+
- Provider/model come from plugin config (`provider`/`model`, default the harness default model).
|
|
34
|
+
|
|
27
35
|
## Task semantics
|
|
28
36
|
|
|
29
37
|
- One domain `righthand_tasks`: a `tasks` table keyed by generated task id (`t-...`).
|
|
@@ -53,5 +61,5 @@ Rules come from the plugin config (`rules: [{ toolPrefix, mode, ask?, destructiv
|
|
|
53
61
|
|
|
54
62
|
## Service availability
|
|
55
63
|
|
|
56
|
-
- **web profile**: all
|
|
64
|
+
- **web profile**: all twenty tools register (`storageDomain` is provided by the web app rows).
|
|
57
65
|
- **other profiles** (e.g. headless): the store and task families stay dormant; the other seven tools still register. The plugin never fails the boot.
|
package/src/index.ts
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
* - store-tools: rh_store_put/get/delete/list over ctx.storageDomain
|
|
16
16
|
* - secrets-tools: rh_credential_describe/set/unset + rh_settings_get/set
|
|
17
17
|
* - task-tools: rh_task_create/list/next/update/delete over ctx.storageDomain
|
|
18
|
+
* - text-tools: rh_text_summarise/extract/classify/translate over ctx.llm
|
|
18
19
|
* - exec-tools: rh_run / rh_run_bg over ctx.subprocess + ctx.jobs
|
|
19
20
|
* - guard-tools: tools/pre-execute policy (config.rules)
|
|
20
21
|
* - skills: the packaged dsh-righthand skill (ctx.skills)
|
|
@@ -28,6 +29,7 @@ import * as secretsTools from './secrets-tools.ts'
|
|
|
28
29
|
import * as execTools from './exec-tools.ts'
|
|
29
30
|
import * as guardTools from './guard-tools.ts'
|
|
30
31
|
import * as taskTools from './task-tools.ts'
|
|
32
|
+
import * as textTools from './text-tools.ts'
|
|
31
33
|
import * as righthandSkills from './skills.ts'
|
|
32
34
|
import type { GuardRule } from './guard-tools.ts'
|
|
33
35
|
|
|
@@ -53,12 +55,13 @@ export function apply(ctx: Context, config: RighthandConfig = {}): void {
|
|
|
53
55
|
ctx.plugin(secretsTools)
|
|
54
56
|
ctx.plugin(execTools)
|
|
55
57
|
ctx.plugin(taskTools)
|
|
58
|
+
ctx.plugin(textTools)
|
|
56
59
|
ctx.plugin(guardTools, { rules: config.rules ?? [] })
|
|
57
60
|
ctx.plugin(righthandSkills)
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
// Individual modules stay importable for selective mounting.
|
|
61
|
-
export { storeTools, secretsTools, execTools, taskTools, guardTools, righthandSkills }
|
|
64
|
+
export { storeTools, secretsTools, execTools, taskTools, textTools, guardTools, righthandSkills }
|
|
62
65
|
export { SKILL_NAME, SKILL_DESCRIPTION, skillDirectory, skillBody } from './skills.ts'
|
|
63
66
|
export type { GuardRule } from './guard-tools.ts'
|
|
64
67
|
export { guardFactsFor } from './guard-tools.ts'
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* text-tools — rh_text_* over ctx.llm.
|
|
3
|
+
* Language work at one call per verb: summarise, extract-to-JSON,
|
|
4
|
+
* classify, translate. Same prompt discipline as the digest summarizer:
|
|
5
|
+
* roles stay roles, caps are deliberate, each call's failure is contained
|
|
6
|
+
* in that call (the other tools are unaffected).
|
|
7
|
+
* @module dsh-righthand/text-tools
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
11
|
+
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
12
|
+
import { BlockAssembler, createUserMessage, deepFreeze } from '@deepseek-ai/dsh-llm'
|
|
13
|
+
import type { GenerateOptions } from '@deepseek-ai/dsh-llm'
|
|
14
|
+
|
|
15
|
+
export const name = 'righthand-text'
|
|
16
|
+
export const inject = ['tools', 'llm']
|
|
17
|
+
|
|
18
|
+
export interface TextConfig {
|
|
19
|
+
/** LLM route; defaults match the harness default model. */
|
|
20
|
+
provider?: string
|
|
21
|
+
model?: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const DEFAULTS: Required<TextConfig> = { provider: 'deepseek-official', model: 'deepseek-v4-flash' }
|
|
25
|
+
|
|
26
|
+
/** One LLM call, text in, text out. Returns the assembled text or throws. */
|
|
27
|
+
async function ask(ctx: Context, cfg: Required<TextConfig>, system: string, user: string, maxTokens: number, signal: AbortSignal): Promise<string> {
|
|
28
|
+
const options: GenerateOptions = deepFreeze({
|
|
29
|
+
provider: cfg.provider,
|
|
30
|
+
model: cfg.model,
|
|
31
|
+
messages: [createUserMessage({
|
|
32
|
+
content: [{ type: 'text', text: user }],
|
|
33
|
+
source: { kind: 'plugin', plugin: 'righthand-text' },
|
|
34
|
+
})],
|
|
35
|
+
system,
|
|
36
|
+
maxTokens,
|
|
37
|
+
purpose: 'compaction',
|
|
38
|
+
signal,
|
|
39
|
+
})
|
|
40
|
+
const assembler = new BlockAssembler()
|
|
41
|
+
for await (const chunk of ctx.llm.stream(options)) assembler.push(chunk)
|
|
42
|
+
if (assembler.finish.kind !== 'stop') throw new Error('text model call did not stop cleanly: ' + JSON.stringify(assembler.finish.kind))
|
|
43
|
+
const text = assembler.blocks().filter((b: any) => b.type === 'text').map((b: any) => b.text).join(' ').trim()
|
|
44
|
+
if (text === '') throw new Error('text model call produced no text')
|
|
45
|
+
return text
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Extract the outermost {...} JSON object from a model reply. */
|
|
49
|
+
function parseJsonWindow(text: string): any {
|
|
50
|
+
const start = text.indexOf('{')
|
|
51
|
+
const end = text.lastIndexOf('}')
|
|
52
|
+
if (start < 0 || end <= start) throw new Error('model output was not JSON')
|
|
53
|
+
return JSON.parse(text.slice(start, end + 1))
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Summarise: plain prose, no JSON. */
|
|
57
|
+
const SUMMARISE_SYSTEM = 'You summarise text. Return ONLY the summary itself - a few sentences, plain prose, no JSON, no Markdown headers, no quotes around it. Preserve the language of the input.'
|
|
58
|
+
|
|
59
|
+
/** Extract: JSON matching a caller schema. */
|
|
60
|
+
const EXTRACT_SYSTEM = 'You extract structured data from text. Return ONLY one JSON object matching the caller schema exactly - no prose, no Markdown, no code fences.'
|
|
61
|
+
|
|
62
|
+
/** Classify: one label plus confidence, as JSON. */
|
|
63
|
+
const CLASSIFY_SYSTEM = 'You classify text. Return ONLY one JSON object with exactly two keys: label (one of the given labels, verbatim) and confidence (a number from 0 to 1). No prose, no Markdown.'
|
|
64
|
+
|
|
65
|
+
/** Translate: preserve formatting, plain prose. */
|
|
66
|
+
const TRANSLATE_SYSTEM = 'You translate text. Return ONLY the translation - plain prose, no JSON, no quotes around it, preserving paragraph breaks and any Markdown or code the input contains.'
|
|
67
|
+
|
|
68
|
+
export function apply(ctx: Context, config: TextConfig = {}): void {
|
|
69
|
+
const cfg: Required<TextConfig> = { ...DEFAULTS, ...config }
|
|
70
|
+
ctx.tools.register(defineTool({
|
|
71
|
+
name: 'rh_text_summarise',
|
|
72
|
+
description: 'Summarise text into a few sentences. One model call per invocation; the result is new prose, not extracted sentences.',
|
|
73
|
+
parameters: {
|
|
74
|
+
text: { type: 'string', required: true, description: 'The text to summarise (max ~30000 characters).' },
|
|
75
|
+
sentences: { type: 'integer', description: 'Target length in sentences (default 3).' },
|
|
76
|
+
},
|
|
77
|
+
output: {
|
|
78
|
+
schema: {
|
|
79
|
+
type: 'object',
|
|
80
|
+
additionalProperties: false,
|
|
81
|
+
properties: {
|
|
82
|
+
summary: { type: 'string', required: true },
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
render: (_args, value) => [{ type: 'text', text: value.summary }],
|
|
86
|
+
},
|
|
87
|
+
async execute(args, exec) {
|
|
88
|
+
const n = args.sentences ?? 3
|
|
89
|
+
const summary = await ask(ctx, cfg, SUMMARISE_SYSTEM, 'Summarise this in about ' + n + ' sentences:\n\n' + args.text, 600, exec.signal)
|
|
90
|
+
return { summary }
|
|
91
|
+
},
|
|
92
|
+
}))
|
|
93
|
+
ctx.tools.register(defineTool({
|
|
94
|
+
name: 'rh_text_extract',
|
|
95
|
+
description: 'Turn text into JSON matching a schema you give. The schema is a JSON Schema object; the model returns one object conforming to it, or the call fails cleanly.',
|
|
96
|
+
parameters: {
|
|
97
|
+
text: { type: 'string', required: true, description: 'The text to extract from.' },
|
|
98
|
+
schema: { type: 'json', required: true, description: 'JSON Schema the result must match, e.g. { "type": "object", "properties": { "name": { "type": "string" } }, "required": ["name"] }.' },
|
|
99
|
+
},
|
|
100
|
+
output: {
|
|
101
|
+
schema: {
|
|
102
|
+
type: 'object',
|
|
103
|
+
additionalProperties: false,
|
|
104
|
+
properties: {
|
|
105
|
+
extracted: { type: 'json', required: true },
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
render: (_args, value) => [{ type: 'text', text: JSON.stringify(value.extracted) }],
|
|
109
|
+
},
|
|
110
|
+
async execute(args, exec) {
|
|
111
|
+
const framed = 'Schema:\n' + JSON.stringify(args.schema) + '\n\nText:\n' + args.text
|
|
112
|
+
const raw = await ask(ctx, cfg, EXTRACT_SYSTEM, framed, 800, exec.signal)
|
|
113
|
+
return { extracted: parseJsonWindow(raw) }
|
|
114
|
+
},
|
|
115
|
+
}))
|
|
116
|
+
ctx.tools.register(defineTool({
|
|
117
|
+
name: 'rh_text_classify',
|
|
118
|
+
description: 'Sort text into one of the labels you give, with a confidence score from 0 to 1. The label returned is one of yours, verbatim.',
|
|
119
|
+
parameters: {
|
|
120
|
+
text: { type: 'string', required: true, description: 'The text to classify.' },
|
|
121
|
+
labels: { type: 'array', required: true, items: { type: 'string' }, description: 'The candidate labels, e.g. ["bug", "feature request", "question"].' },
|
|
122
|
+
},
|
|
123
|
+
output: {
|
|
124
|
+
schema: {
|
|
125
|
+
type: 'object',
|
|
126
|
+
additionalProperties: false,
|
|
127
|
+
properties: {
|
|
128
|
+
label: { type: 'string', required: true },
|
|
129
|
+
confidence: { type: 'number', required: true },
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
render: (_args, value) => [{ type: 'text', text: value.label + ' (' + value.confidence + ')' }],
|
|
133
|
+
},
|
|
134
|
+
async execute(args, exec) {
|
|
135
|
+
if (args.labels.length === 0) throw new Error('classify needs at least one label')
|
|
136
|
+
const framed = 'Labels: ' + JSON.stringify(args.labels) + '\n\nText:\n' + args.text
|
|
137
|
+
const raw = await ask(ctx, cfg, CLASSIFY_SYSTEM, framed, 100, exec.signal)
|
|
138
|
+
const parsed = parseJsonWindow(raw)
|
|
139
|
+
if (typeof parsed.label !== 'string' || !args.labels.includes(parsed.label)) throw new Error('model returned a label outside the given set')
|
|
140
|
+
const confidence = Number(parsed.confidence)
|
|
141
|
+
if (!Number.isFinite(confidence) || confidence < 0 || confidence > 1) throw new Error('model returned an invalid confidence')
|
|
142
|
+
return { label: parsed.label, confidence }
|
|
143
|
+
},
|
|
144
|
+
}))
|
|
145
|
+
ctx.tools.register(defineTool({
|
|
146
|
+
name: 'rh_text_translate',
|
|
147
|
+
description: 'Translate text into another language, preserving formatting (paragraph breaks, Markdown, code).',
|
|
148
|
+
parameters: {
|
|
149
|
+
text: { type: 'string', required: true, description: 'The text to translate.' },
|
|
150
|
+
language: { type: 'string', required: true, description: 'The target language, e.g. "Spanish", "zh-CN", "Japanese".' },
|
|
151
|
+
},
|
|
152
|
+
output: {
|
|
153
|
+
schema: {
|
|
154
|
+
type: 'object',
|
|
155
|
+
additionalProperties: false,
|
|
156
|
+
properties: {
|
|
157
|
+
translation: { type: 'string', required: true },
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
render: (_args, value) => [{ type: 'text', text: value.translation }],
|
|
161
|
+
},
|
|
162
|
+
async execute(args, exec) {
|
|
163
|
+
const framed = 'Translate the following into ' + args.language + ':\n\n' + args.text
|
|
164
|
+
const translation = await ask(ctx, cfg, TRANSLATE_SYSTEM, framed, 800, exec.signal)
|
|
165
|
+
return { translation }
|
|
166
|
+
},
|
|
167
|
+
}))
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|