@things-factory/figure-service 10.1.3 → 10.1.6
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/coverage/clover.xml +6 -0
- package/coverage/coverage-final.json +1 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +101 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +210 -0
- package/coverage/lcov.info +0 -0
- package/dist-server/index.d.ts +2 -0
- package/dist-server/index.js +13 -0
- package/dist-server/index.js.map +1 -1
- package/dist-server/routers/figure-thumbnail-router.d.ts +24 -0
- package/dist-server/routers/figure-thumbnail-router.js +93 -0
- package/dist-server/routers/figure-thumbnail-router.js.map +1 -0
- package/dist-server/routes.d.ts +1 -0
- package/dist-server/routes.js +21 -0
- package/dist-server/routes.js.map +1 -0
- package/dist-server/service/figure/event-subscriber.d.ts +21 -0
- package/dist-server/service/figure/event-subscriber.js +51 -0
- package/dist-server/service/figure/event-subscriber.js.map +1 -0
- package/dist-server/service/figure/figure-gate.d.ts +55 -0
- package/dist-server/service/figure/figure-gate.js +64 -0
- package/dist-server/service/figure/figure-gate.js.map +1 -0
- package/dist-server/service/figure/figure-history.d.ts +43 -0
- package/dist-server/service/figure/figure-history.js +155 -0
- package/dist-server/service/figure/figure-history.js.map +1 -0
- package/dist-server/service/figure/figure-mutation.d.ts +45 -0
- package/dist-server/service/figure/figure-mutation.js +174 -1
- package/dist-server/service/figure/figure-mutation.js.map +1 -1
- package/dist-server/service/figure/figure-query.d.ts +24 -1
- package/dist-server/service/figure/figure-query.js +75 -0
- package/dist-server/service/figure/figure-query.js.map +1 -1
- package/dist-server/service/figure/figure-tools.d.ts +10 -0
- package/dist-server/service/figure/figure-tools.js +304 -0
- package/dist-server/service/figure/figure-tools.js.map +1 -0
- package/dist-server/service/figure/figure-type.d.ts +25 -1
- package/dist-server/service/figure/figure-type.js +58 -5
- package/dist-server/service/figure/figure-type.js.map +1 -1
- package/dist-server/service/figure/figure.d.ts +9 -0
- package/dist-server/service/figure/figure.js +15 -3
- package/dist-server/service/figure/figure.js.map +1 -1
- package/dist-server/service/figure/index.d.ts +4 -1
- package/dist-server/service/figure/index.js +5 -2
- package/dist-server/service/figure/index.js.map +1 -1
- package/dist-server/service/index.d.ts +6 -2
- package/dist-server/service/index.js +17 -2
- package/dist-server/service/index.js.map +1 -1
- package/dist-server/tsconfig.tsbuildinfo +1 -0
- package/package.json +9 -6
- package/server/index.ts +13 -0
- package/server/routers/figure-thumbnail-router.ts +110 -0
- package/server/routes.ts +24 -0
- package/server/service/figure/event-subscriber.ts +49 -0
- package/server/service/figure/figure-gate.ts +119 -0
- package/server/service/figure/figure-history.ts +130 -0
- package/server/service/figure/figure-mutation.ts +180 -1
- package/server/service/figure/figure-propose.test.ts +136 -4
- package/server/service/figure/figure-query.ts +63 -1
- package/server/service/figure/figure-tools.test.ts +284 -0
- package/server/service/figure/figure-tools.ts +320 -0
- package/server/service/figure/figure-type.ts +49 -2
- package/server/service/figure/figure.ts +15 -3
- package/server/service/figure/index.ts +4 -1
- package/server/service/index.ts +23 -2
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 저작 도구의 **배선**을 못박는다.
|
|
3
|
+
*
|
|
4
|
+
* 도구가 무엇을 계산하는지는 각 진입점의 시험이 이미 잠근다(`figure-propose.test` ·
|
|
5
|
+
* figure-model 의 게이트 시험). 비어 있던 것은 **그 사이를 잇는 줄들**이고, 그 줄이 끊어질 때
|
|
6
|
+
* 나타나는 증상이 실제로 겪은 실패와 1:1 로 대응한다.
|
|
7
|
+
*
|
|
8
|
+
* · guidance → 도구 호출 끊기면 → 「후보를 만들었습니다」라고만 쓰고 화면에는 아무것도 없다
|
|
9
|
+
* · hostContext.figureId → 대상 끊기면 → 화면에 도형이 떠 있는데 「어느 도형입니까」를 되묻는다
|
|
10
|
+
* · 도메인 확인 → 대상 끊기면 → 모델이 넘긴 id 로 남의 테넌트를 읽는다
|
|
11
|
+
* · hostContext.palette → 후보 끊기면 → 모델이 색을 지어내고 그리는 시점에 죽는다
|
|
12
|
+
*
|
|
13
|
+
* 실제 모델은 부르지 않는다. 여기서 볼 것은 모델의 솜씨가 아니라 **우리 배선**이다.
|
|
14
|
+
*/
|
|
15
|
+
import { clearToolRegistry, getToolCategories } from '@things-factory/ai-client-base'
|
|
16
|
+
|
|
17
|
+
import { __setFigureToolDepsForTest, registerFigureToolCategories } from './figure-tools'
|
|
18
|
+
|
|
19
|
+
/** 도메인 소속을 흉내내는 저장소. 넘긴 id 와 도메인이 맞을 때만 돌려준다. */
|
|
20
|
+
function stubRepository(rows: any[]) {
|
|
21
|
+
return {
|
|
22
|
+
async findOne({ where }: any) {
|
|
23
|
+
return (
|
|
24
|
+
rows.find(row => row.id === where.id && row.domain?.id === where.domain?.id) ?? null
|
|
25
|
+
)
|
|
26
|
+
},
|
|
27
|
+
async find({ where }: any) {
|
|
28
|
+
return rows.filter(row => row.domain?.id === where.domain?.id)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const MINE = { id: 'domain-mine' }
|
|
34
|
+
const THEIRS = { id: 'domain-theirs' }
|
|
35
|
+
|
|
36
|
+
const SOURCE = {
|
|
37
|
+
type: 'MIXER',
|
|
38
|
+
base: { x: 300, y: 500, z: 300 },
|
|
39
|
+
detailLevel: 'M',
|
|
40
|
+
parts: [
|
|
41
|
+
{
|
|
42
|
+
name: 'body',
|
|
43
|
+
primitive: 'cube',
|
|
44
|
+
transform: { position: { x: 0, y: 0, z: 0 }, size: { x: 300, y: 400, z: 300 } },
|
|
45
|
+
material: { token: 'palette.primary' }
|
|
46
|
+
}
|
|
47
|
+
]
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const ROWS = [
|
|
51
|
+
{ id: 'fig-1', type: 'MIXER', name: '믹서', state: 'draft', domain: MINE, source: JSON.stringify(SOURCE) },
|
|
52
|
+
{ id: 'fig-blank', type: 'EMPTY', name: '빈 것', state: 'draft', domain: MINE, source: null },
|
|
53
|
+
{ id: 'fig-other', type: 'SECRET', name: '남의 것', state: 'draft', domain: THEIRS, source: JSON.stringify(SOURCE) }
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
/** 부른 것을 적어 두는 가짜 제안기. */
|
|
57
|
+
const proposed: any[] = []
|
|
58
|
+
|
|
59
|
+
function install() {
|
|
60
|
+
const model = require('@hatiolab/figure-model')
|
|
61
|
+
__setFigureToolDepsForTest({
|
|
62
|
+
getRepository: () => stubRepository(ROWS),
|
|
63
|
+
Figure: class {},
|
|
64
|
+
inspectSource: (json?: string | null) =>
|
|
65
|
+
json ? { blocked: false, findings: [] } : { blocked: true, findings: [{ code: 'no-source', message: '정본이 없다', blocking: true }] },
|
|
66
|
+
proposeFigure: async (input: any) => {
|
|
67
|
+
proposed.push(input)
|
|
68
|
+
return {
|
|
69
|
+
source: SOURCE,
|
|
70
|
+
score: { grade: 'B' },
|
|
71
|
+
cost: { triangles: 12, at: { drawCalls: 100 } },
|
|
72
|
+
violations: [],
|
|
73
|
+
attempts: 1
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
compile: model.compile,
|
|
77
|
+
costOf: model.costOf,
|
|
78
|
+
scoreOf: model.scoreOf
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** 등록된 범주에서 도구 하나를 꺼낸다. */
|
|
83
|
+
function toolNamed(name: string) {
|
|
84
|
+
const category = getToolCategories().find((one: any) => one.name === 'figure-authoring')
|
|
85
|
+
if (!category) throw new Error('figure-authoring 범주가 등록되지 않았다')
|
|
86
|
+
const spec = category.specs.find((one: any) => one.name === name)
|
|
87
|
+
if (!spec) throw new Error(`도구 ${name} 이 없다`)
|
|
88
|
+
return spec as any
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const ctx = (host: any = { figureId: 'fig-1', palette: ['palette.primary'] }) => ({
|
|
92
|
+
state: { domain: MINE, host }
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
beforeEach(() => {
|
|
96
|
+
clearToolRegistry()
|
|
97
|
+
proposed.length = 0
|
|
98
|
+
install()
|
|
99
|
+
registerFigureToolCategories()
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
afterEach(() => clearToolRegistry())
|
|
103
|
+
|
|
104
|
+
describe('범주가 등록된다', () => {
|
|
105
|
+
it('figure-authoring 이 도구 넷과 함께 올라온다', () => {
|
|
106
|
+
const category = getToolCategories().find((one: any) => one.name === 'figure-authoring')!
|
|
107
|
+
|
|
108
|
+
expect(category.specs.map((one: any) => one.name).sort()).toEqual([
|
|
109
|
+
'getFigureSource',
|
|
110
|
+
'inspectFigure',
|
|
111
|
+
'listFigures',
|
|
112
|
+
'proposeFigureCandidate'
|
|
113
|
+
])
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it('전부 조회 전용이다 — 되돌릴 수 없는 조작이 대화면으로 새지 않는다', () => {
|
|
117
|
+
const category = getToolCategories().find((one: any) => one.name === 'figure-authoring')!
|
|
118
|
+
|
|
119
|
+
expect(category.specs.map((one: any) => one.kind)).toEqual(['read', 'read', 'read', 'read'])
|
|
120
|
+
})
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
/*
|
|
124
|
+
twin 이 이 자리에서 사고를 냈다. 조치 요청에 도구를 부르지 않고 「제안해 두었습니다」라고만 답해
|
|
125
|
+
사용자에게 없는 버튼을 누르라고 안내했다. figure 도 같은 구조다 — 후보 칸은 도구 호출 결과에서만
|
|
126
|
+
만들어진다. 그래서 규율을 도구와 함께 보내고, 그 규율이 실려 있는지를 여기서 못박는다.
|
|
127
|
+
*/
|
|
128
|
+
describe('규율이 도구와 함께 나간다', () => {
|
|
129
|
+
it('후보는 도구 호출로만 생긴다는 것을 규율이 말한다', () => {
|
|
130
|
+
const category = getToolCategories().find((one: any) => one.name === 'figure-authoring')!
|
|
131
|
+
|
|
132
|
+
expect(category.guidance).toBeTruthy()
|
|
133
|
+
expect(category.guidance).toContain('proposeFigureCandidate')
|
|
134
|
+
expect(category.guidance).toMatch(/거짓/)
|
|
135
|
+
expect(category.guidance).toMatch(/도구 호출 결과에서만/)
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
it('고치기 전에 정본을 읽으라고 말한다 — 이름이 갈리면 배치된 보드가 끊긴다', () => {
|
|
139
|
+
const category = getToolCategories().find((one: any) => one.name === 'figure-authoring')!
|
|
140
|
+
|
|
141
|
+
expect(category.guidance).toContain('getFigureSource')
|
|
142
|
+
expect(category.guidance).toMatch(/바인딩이 끊긴다/)
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
it('타입 이름을 지어내지 말라고 말한다 — 저장되는 식별자다', () => {
|
|
146
|
+
const category = getToolCategories().find((one: any) => one.name === 'figure-authoring')!
|
|
147
|
+
|
|
148
|
+
expect(category.guidance).toContain('listFigures')
|
|
149
|
+
expect(category.guidance).toMatch(/저장되는 식별자/)
|
|
150
|
+
})
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
describe('대화면 문맥에서 대상을 해소한다', () => {
|
|
154
|
+
it('id 를 안 주면 열려 있는 도형을 읽는다 — 사용자에게 되묻지 않는다', async () => {
|
|
155
|
+
const result = await toolNamed('getFigureSource').builder({}, ctx())
|
|
156
|
+
|
|
157
|
+
expect(result.id).toBe('fig-1')
|
|
158
|
+
expect(result.source.type).toBe('MIXER')
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
it('열린 도형도 없고 id 도 없으면 명시 실패한다 — 아무 도형이나 고르지 않는다', async () => {
|
|
162
|
+
await expect(toolNamed('getFigureSource').builder({}, ctx({}))).rejects.toThrow(/figureId is required/)
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
it('정본이 없는 도형은 없다고 말한다 — 지어내지 않는다', async () => {
|
|
166
|
+
const result = await toolNamed('getFigureSource').builder({ figureId: 'fig-blank' }, ctx())
|
|
167
|
+
|
|
168
|
+
expect(result.source).toBeNull()
|
|
169
|
+
expect(result.cost).toBeNull()
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
it('측정한 비용이 함께 온다 — 눈대중으로 세지 않게', async () => {
|
|
173
|
+
const result = await toolNamed('getFigureSource').builder({ figureId: 'fig-1' }, ctx())
|
|
174
|
+
|
|
175
|
+
expect(result.cost.triangles).toBeGreaterThan(0)
|
|
176
|
+
expect(result.cost.drawCalls).toBeGreaterThan(0)
|
|
177
|
+
})
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
/*
|
|
181
|
+
사람이 목록에서 고르는 길에서는 드러나지 않는다. 모델은 대화 내용에 이끌려 임의의 id 를 넘길 수
|
|
182
|
+
있고, 그것을 그대로 믿으면 남의 테넌트를 읽는다.
|
|
183
|
+
*/
|
|
184
|
+
describe('모델이 넘긴 id 를 믿지 않는다', () => {
|
|
185
|
+
it('다른 도메인의 도형은 못 찾았다고 한다', async () => {
|
|
186
|
+
await expect(toolNamed('getFigureSource').builder({ figureId: 'fig-other' }, ctx())).rejects.toThrow(
|
|
187
|
+
/not found in this tenant/
|
|
188
|
+
)
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
it('발행 판정도 같은 확인을 지난다', async () => {
|
|
192
|
+
await expect(toolNamed('inspectFigure').builder({ figureId: 'fig-other' }, ctx())).rejects.toThrow(
|
|
193
|
+
/not found in this tenant/
|
|
194
|
+
)
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
it('목록은 자기 도메인만 낸다', async () => {
|
|
198
|
+
const result = await toolNamed('listFigures').builder({}, ctx())
|
|
199
|
+
|
|
200
|
+
expect(result.figures.map((one: any) => one.id)).toEqual(['fig-1', 'fig-blank'])
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
it('도메인 문맥이 없으면 아무것도 읽지 않는다', async () => {
|
|
204
|
+
await expect(toolNamed('listFigures').builder({}, { state: {} })).rejects.toThrow(/domain context is missing/)
|
|
205
|
+
})
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
describe('후보를 요청할 때', () => {
|
|
209
|
+
it('열린 도형이 있으면 고치는 것으로 본다 — 그 정본이 함께 나간다', async () => {
|
|
210
|
+
await toolNamed('proposeFigureCandidate').builder({ prompt: '재질을 줄여줘' }, ctx())
|
|
211
|
+
|
|
212
|
+
expect(proposed).toHaveLength(1)
|
|
213
|
+
expect(proposed[0].base.type).toBe('MIXER')
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
it('create 를 켜면 열린 도형이 있어도 새로 만든다 — 열린 것을 덮어쓰는 후보가 오지 않게', async () => {
|
|
217
|
+
await toolNamed('proposeFigureCandidate').builder({ prompt: '새 컨베이어', create: true, type: 'CONVEYOR' }, ctx())
|
|
218
|
+
|
|
219
|
+
expect(proposed[0].base).toBeUndefined()
|
|
220
|
+
expect(proposed[0].type).toBe('CONVEYOR')
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
/*
|
|
224
|
+
팔레트를 모르면 모델이 색을 지어낸다. 그 후보는 형식 검증을 지나가고 **그리는 시점에**
|
|
225
|
+
`resolveToken` 이 던진다 — 저장까지 되고 나서 화면에서 죽는다. 빈 목록으로 진행하지 않는다.
|
|
226
|
+
*/
|
|
227
|
+
it('팔레트가 문맥에 없으면 후보를 요청하지 않는다', async () => {
|
|
228
|
+
await expect(
|
|
229
|
+
toolNamed('proposeFigureCandidate').builder({ prompt: '무언가' }, ctx({ figureId: 'fig-1' }))
|
|
230
|
+
).rejects.toThrow(/palette is missing/)
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
it('빈 요청은 거절한다', async () => {
|
|
234
|
+
await expect(toolNamed('proposeFigureCandidate').builder({ prompt: ' ' }, ctx())).rejects.toThrow(
|
|
235
|
+
/prompt is required/
|
|
236
|
+
)
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
it('남의 도형을 고치라고 하면 못 찾았다고 한다', async () => {
|
|
240
|
+
await expect(
|
|
241
|
+
toolNamed('proposeFigureCandidate').builder({ prompt: '고쳐줘', figureId: 'fig-other' }, ctx())
|
|
242
|
+
).rejects.toThrow(/not found in this tenant/)
|
|
243
|
+
})
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
/*
|
|
247
|
+
후보가 화면에 닿는 길은 하나다 — 도구 결과의 `proposed: true` 가 카드를 만들고, 카드의 단추가
|
|
248
|
+
호스트에게 실행을 넘긴다. 이 표시가 빠지면 후보는 도구 추적 안에 묻히고 화면에는 아무것도 생기지
|
|
249
|
+
않는다. 그때 모델이 「후보를 만들었습니다」라고 쓴 것이 거짓이 된다. twin 이 이 자리에서 사용자에게
|
|
250
|
+
없는 단추를 누르라고 안내했다.
|
|
251
|
+
*/
|
|
252
|
+
describe('후보가 카드가 된다', () => {
|
|
253
|
+
it('proposed 표시가 붙어 온다 — 없으면 화면에 아무것도 생기지 않는다', async () => {
|
|
254
|
+
const out = await toolNamed('proposeFigureCandidate').builder({ prompt: '교반기', create: true }, ctx())
|
|
255
|
+
|
|
256
|
+
expect(out.proposed).toBe(true)
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
it('카드에 그릴 두 줄이 함께 온다', async () => {
|
|
260
|
+
const out = await toolNamed('proposeFigureCandidate').builder({ prompt: '교반기', create: true }, ctx())
|
|
261
|
+
|
|
262
|
+
expect(out.label).toContain('MIXER')
|
|
263
|
+
// 잰 값을 그대로 적는다 — 「좋아졌다」로 요약하면 저작자가 판단할 근거가 없다.
|
|
264
|
+
expect(out.reason).toMatch(/등급 B/)
|
|
265
|
+
expect(out.reason).toMatch(/삼각형 12/)
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
it('후보 자체와 측정값이 함께 온다 — 호스트가 두 칸을 그릴 수 있게', async () => {
|
|
269
|
+
const out = await toolNamed('proposeFigureCandidate').builder({ prompt: '교반기', create: true }, ctx())
|
|
270
|
+
|
|
271
|
+
expect(out.source.type).toBe('MIXER')
|
|
272
|
+
expect(out.score.grade).toBe('B')
|
|
273
|
+
expect(out.cost.triangles).toBe(12)
|
|
274
|
+
expect(out.attempts).toBe(1)
|
|
275
|
+
})
|
|
276
|
+
|
|
277
|
+
it('고치는 것과 만드는 것을 이름으로 가른다', async () => {
|
|
278
|
+
const revise = await toolNamed('proposeFigureCandidate').builder({ prompt: '고쳐줘' }, ctx())
|
|
279
|
+
const create = await toolNamed('proposeFigureCandidate').builder({ prompt: '만들어줘', create: true }, ctx())
|
|
280
|
+
|
|
281
|
+
expect(revise.label).toMatch(/고치기/)
|
|
282
|
+
expect(create.label).toMatch(/만들기/)
|
|
283
|
+
})
|
|
284
|
+
})
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* figure 저작 도구를 어시스턴트 도구 레지스트리에 등록 — **대화면(도크)에서 저작을 돕기 위함.**
|
|
3
|
+
*
|
|
4
|
+
* 왜 필요한가: `proposeFigure` 는 한 번 시키고 한 번 받는다. 그것으로 되는 일이 있지만, 저작은
|
|
5
|
+
* 대개 「이걸 이렇게 고쳐 봐 → 무게가 얼마나 되나 → 발행은 되나」처럼 여러 차례 오간다. 그 대화를
|
|
6
|
+
* 하려면 어시스턴트가 **지금 무엇이 있는지 읽을 수 있어야** 한다. 읽지 못하면 매번 사람이 정본을
|
|
7
|
+
* 붙여 넣어야 하고, 그러면 대화가 아니다.
|
|
8
|
+
*
|
|
9
|
+
* 능력을 복제하지 않는다. figure-service 가 이미 가진 진입점(`inspectSource` · `proposeFigure` ·
|
|
10
|
+
* 조회)을 레지스트리에 투영한다.
|
|
11
|
+
*
|
|
12
|
+
* ── 전부 조회·제안 전용이다 ─────────────────────────────────────────────────
|
|
13
|
+
* 정본을 고치는 도구를 넣지 않는다. 형식의 설계가 그렇게 정해져 있다 — 어시스턴트는 **후보**를
|
|
14
|
+
* 내고, 받을지 버릴지는 저작자가 정한다(`figure-propose.ts`). 되돌릴 수 없는 조작이 대화면으로
|
|
15
|
+
* 새지 않으므로 전부 `kind: 'read'` 다.
|
|
16
|
+
*
|
|
17
|
+
* 저장은 저작면이 `updateFigure` 로 한다. 그 mutation 은 `figure:mutation` 이 지키고, 대화는
|
|
18
|
+
* `ai-assistant:mutation` 이 지킨다 — 서로 다른 질문이라 서로 다른 권한이 지킨다. figure 가 board
|
|
19
|
+
* 의 권한을 빌리지 않는다.
|
|
20
|
+
*
|
|
21
|
+
* ── 규율을 도구와 함께 보낸다 ───────────────────────────────────────────────
|
|
22
|
+
* 도구 목록만으로는 「언제 반드시 불러야 하는지」를 모른다. twin 이 그 공백에서 사고를 냈다 —
|
|
23
|
+
* 조치 요청에 도구를 부르지 않고 「제안해 두었습니다」라고만 답해, 사용자에게 **없는 버튼을 누르라고
|
|
24
|
+
* 안내했다.** figure 도 똑같은 자리가 있다: 후보 화면(「지금 / 후보」 두 칸)은 **도구 호출 결과에서만**
|
|
25
|
+
* 만들어진다. 문장으로는 아무 일도 일어나지 않는다.
|
|
26
|
+
*
|
|
27
|
+
* ── LLM 이 넘긴 id 를 믿지 않는다 ───────────────────────────────────────────
|
|
28
|
+
* 사람이 목록에서 고르는 길에서는 드러나지 않지만, 모델은 대화 내용에 이끌려 임의의 id 를 넘길 수
|
|
29
|
+
* 있다. 그래서 호출자 도메인 소속을 먼저 확인한다.
|
|
30
|
+
*/
|
|
31
|
+
import { registerToolCategory, type ToolCategory, type ToolSpec } from '@things-factory/ai-client-base'
|
|
32
|
+
|
|
33
|
+
/*
|
|
34
|
+
서버 의존성은 lazy require 다. 등록은 부팅 초반에 평가되고 그때 typeorm DataSource 가 준비되지
|
|
35
|
+
않았을 수 있다(board-import · twin-ai 와 같은 정책).
|
|
36
|
+
*/
|
|
37
|
+
let _deps: any | undefined
|
|
38
|
+
|
|
39
|
+
function getDeps(): any {
|
|
40
|
+
if (!_deps) {
|
|
41
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
42
|
+
const shell = require('@things-factory/shell')
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
44
|
+
const model = require('@hatiolab/figure-model')
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
46
|
+
const entity = require('./figure.js')
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
48
|
+
const gate = require('./figure-gate.js')
|
|
49
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
50
|
+
const propose = require('./figure-propose.js')
|
|
51
|
+
_deps = {
|
|
52
|
+
getRepository: shell.getRepository,
|
|
53
|
+
Figure: entity.Figure,
|
|
54
|
+
inspectSource: gate.inspectSource,
|
|
55
|
+
proposeFigure: propose.proposeFigure,
|
|
56
|
+
compile: model.compile,
|
|
57
|
+
costOf: model.costOf,
|
|
58
|
+
scoreOf: model.scoreOf
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return _deps
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** 시험·검증 하네스 전용 주입 시임. 운영 경로에서 호출 금지. */
|
|
65
|
+
export function __setFigureToolDepsForTest(deps: any): void {
|
|
66
|
+
_deps = deps
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function requireDomainId(state: any): string {
|
|
70
|
+
const id = state?.domain?.id
|
|
71
|
+
if (!id) {
|
|
72
|
+
// 도메인을 모르면 무엇을 읽어도 남의 것일 수 있다. 조용히 넘기지 않는다.
|
|
73
|
+
throw new Error('domain context is missing — the figure tools cannot resolve tenant scope')
|
|
74
|
+
}
|
|
75
|
+
return id
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 이 도형이 호출자 도메인 것인가.
|
|
80
|
+
*
|
|
81
|
+
* 모델이 넘긴 id 를 그대로 쓰지 않는다. 없으면 못 찾았다고 말한다 — 「권한이 없다」와 구별해
|
|
82
|
+
* 알려 줄 것이 없다(다른 테넌트에 그 id 가 있다는 사실 자체가 알려 줄 것이 아니다).
|
|
83
|
+
*/
|
|
84
|
+
async function figureInDomain(figureId: string, domainId: string): Promise<any> {
|
|
85
|
+
const { getRepository, Figure } = getDeps()
|
|
86
|
+
const found = await getRepository(Figure).findOne({
|
|
87
|
+
where: { id: figureId, domain: { id: domainId } }
|
|
88
|
+
})
|
|
89
|
+
if (!found) {
|
|
90
|
+
throw new Error(`figure not found in this tenant: ${figureId}`)
|
|
91
|
+
}
|
|
92
|
+
return found
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 무엇을 고치라는 것인지 대화면 문맥에서 해소한다.
|
|
97
|
+
*
|
|
98
|
+
* 모델이 대상을 안 줬으면 **저작면이 지금 열고 있는 도형**이다. 사용자에게 식별자를 되묻지
|
|
99
|
+
* 않는다 — 화면에 그 도형이 떠 있는데 「어느 도형입니까」를 묻는 것은 문맥을 안 읽은 것이다.
|
|
100
|
+
*/
|
|
101
|
+
function figureIdFrom(args: any, host: any): string {
|
|
102
|
+
const said = typeof args?.figureId === 'string' ? args.figureId.trim() : ''
|
|
103
|
+
if (said) return said
|
|
104
|
+
|
|
105
|
+
const open = typeof host?.figureId === 'string' ? host.figureId.trim() : ''
|
|
106
|
+
if (open) return open
|
|
107
|
+
|
|
108
|
+
throw new Error('figureId is required: this surface has no figure open to resolve it from')
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const FIGURE_ID_ARG = {
|
|
112
|
+
figureId: {
|
|
113
|
+
type: 'string',
|
|
114
|
+
description: 'The figure to read. Omit to use the one open in the editor.'
|
|
115
|
+
}
|
|
116
|
+
} as const
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 도구 넷.
|
|
120
|
+
*
|
|
121
|
+
* 목록 · 정본 · 발행 판정 · 후보. 저작 대화에서 실제로 묻는 것이 그 넷이다 — 「무엇이 있나」
|
|
122
|
+
* 「지금 어떻게 생겼나」 「발행이 왜 안 되나」 「이렇게 고쳐 봐」.
|
|
123
|
+
*/
|
|
124
|
+
function figureToolSpecs(): ToolSpec[] {
|
|
125
|
+
return [
|
|
126
|
+
{
|
|
127
|
+
kind: 'read',
|
|
128
|
+
name: 'listFigures',
|
|
129
|
+
description:
|
|
130
|
+
'Lists the figures in this tenant with their type names, states and part counts. ' +
|
|
131
|
+
'Call it before naming a type — type names are stored identifiers and must not be invented.',
|
|
132
|
+
schema: { type: 'object', properties: {} },
|
|
133
|
+
builder: async (_args: any, ctx: any) => {
|
|
134
|
+
const domainId = requireDomainId(ctx?.state)
|
|
135
|
+
const { getRepository, Figure } = getDeps()
|
|
136
|
+
const rows = await getRepository(Figure).find({
|
|
137
|
+
where: { domain: { id: domainId } },
|
|
138
|
+
order: { updatedAt: 'DESC' },
|
|
139
|
+
take: 200
|
|
140
|
+
})
|
|
141
|
+
return {
|
|
142
|
+
figures: rows.map((row: any) => ({
|
|
143
|
+
id: row.id,
|
|
144
|
+
type: row.type,
|
|
145
|
+
name: row.name,
|
|
146
|
+
state: row.state,
|
|
147
|
+
score: row.score,
|
|
148
|
+
triangles: row.triangles
|
|
149
|
+
}))
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
kind: 'read',
|
|
155
|
+
name: 'getFigureSource',
|
|
156
|
+
description:
|
|
157
|
+
'Returns the canonical FigureSource of one figure, with its measured cost ' +
|
|
158
|
+
'(triangles, draw calls, how many fit on one board). Call it before proposing a revision — ' +
|
|
159
|
+
'revising without reading it means proposing against a guess.',
|
|
160
|
+
schema: { type: 'object', properties: { ...FIGURE_ID_ARG } },
|
|
161
|
+
builder: async (args: any, ctx: any) => {
|
|
162
|
+
const domainId = requireDomainId(ctx?.state)
|
|
163
|
+
const figureId = figureIdFrom(args, ctx?.state?.host)
|
|
164
|
+
const row = await figureInDomain(figureId, domainId)
|
|
165
|
+
|
|
166
|
+
if (!row.source) {
|
|
167
|
+
// 저장된 정본이 없는 상태가 실제로 있다(새로 만들고 저장 안 한 것). 지어내지 않는다.
|
|
168
|
+
return { id: row.id, type: row.type, name: row.name, source: null, cost: null }
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const { compile, costOf, scoreOf } = getDeps()
|
|
172
|
+
const source = JSON.parse(row.source)
|
|
173
|
+
const blueprint = compile(source)
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
id: row.id,
|
|
177
|
+
type: row.type,
|
|
178
|
+
name: row.name,
|
|
179
|
+
state: row.state,
|
|
180
|
+
source,
|
|
181
|
+
cost: costOf(blueprint, 100),
|
|
182
|
+
score: scoreOf(blueprint)
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
kind: 'read',
|
|
188
|
+
name: 'inspectFigure',
|
|
189
|
+
description:
|
|
190
|
+
'Says whether a figure may be released and, when it may not, what is wrong — each finding ' +
|
|
191
|
+
'carries why it is a problem and what to change. Call it when asked about releasing, or ' +
|
|
192
|
+
'when asked to fix a figure that will not release.',
|
|
193
|
+
schema: { type: 'object', properties: { ...FIGURE_ID_ARG } },
|
|
194
|
+
builder: async (args: any, ctx: any) => {
|
|
195
|
+
const domainId = requireDomainId(ctx?.state)
|
|
196
|
+
const figureId = figureIdFrom(args, ctx?.state?.host)
|
|
197
|
+
const row = await figureInDomain(figureId, domainId)
|
|
198
|
+
|
|
199
|
+
const { inspectSource } = getDeps()
|
|
200
|
+
return inspectSource(row.source)
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
kind: 'read',
|
|
205
|
+
name: 'proposeFigureCandidate',
|
|
206
|
+
description:
|
|
207
|
+
'Asks for a figure candidate from a description, optionally revising an existing one. ' +
|
|
208
|
+
'Returns the candidate with its score and cost. NOTHING IS SAVED — the author accepts or ' +
|
|
209
|
+
'discards it on screen. This is the only way a candidate reaches the screen.',
|
|
210
|
+
schema: {
|
|
211
|
+
type: 'object',
|
|
212
|
+
properties: {
|
|
213
|
+
prompt: {
|
|
214
|
+
type: 'string',
|
|
215
|
+
description: 'What to make or change, in the words the author used.'
|
|
216
|
+
},
|
|
217
|
+
figureId: {
|
|
218
|
+
type: 'string',
|
|
219
|
+
description: 'Revise this figure. Omit to revise the one open in the editor; pass null to create anew.'
|
|
220
|
+
},
|
|
221
|
+
type: {
|
|
222
|
+
type: 'string',
|
|
223
|
+
description: 'Type name for a new figure. Check listFigures first — it is a stored identifier.'
|
|
224
|
+
},
|
|
225
|
+
create: {
|
|
226
|
+
type: 'boolean',
|
|
227
|
+
description: 'True to create rather than revise, even with a figure open.'
|
|
228
|
+
}
|
|
229
|
+
},
|
|
230
|
+
required: ['prompt']
|
|
231
|
+
},
|
|
232
|
+
builder: async (args: any, ctx: any) => {
|
|
233
|
+
const domainId = requireDomainId(ctx?.state)
|
|
234
|
+
const prompt = typeof args?.prompt === 'string' ? args.prompt.trim() : ''
|
|
235
|
+
if (!prompt) throw new Error('prompt is required: there is nothing to ask for')
|
|
236
|
+
|
|
237
|
+
/*
|
|
238
|
+
고치는 것인지 만드는 것인지를 **문맥에서 정한다.** 열린 도형이 있으면 고치는 것이 기본이고,
|
|
239
|
+
모델이 `create` 를 켜면 만드는 것이다. 이것을 모델의 짐작에 맡기면 열려 있는 도형을 덮어쓰는
|
|
240
|
+
후보가 온다.
|
|
241
|
+
*/
|
|
242
|
+
let base: unknown
|
|
243
|
+
if (!args?.create) {
|
|
244
|
+
const open = typeof args?.figureId === 'string' ? args.figureId : ctx?.state?.host?.figureId
|
|
245
|
+
if (typeof open === 'string' && open.trim()) {
|
|
246
|
+
const row = await figureInDomain(open.trim(), domainId)
|
|
247
|
+
if (row.source) base = JSON.parse(row.source)
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const palette = Array.isArray(ctx?.state?.host?.palette) ? ctx.state.host.palette : []
|
|
252
|
+
if (palette.length === 0) {
|
|
253
|
+
/*
|
|
254
|
+
팔레트를 모르면 모델이 색을 지어내고, 그 후보는 그리는 시점에 `resolveToken` 이 던진다 —
|
|
255
|
+
저장까지 되고 나서 화면에서 죽는다. 빈 목록으로 진행하지 않는다.
|
|
256
|
+
*/
|
|
257
|
+
throw new Error('the palette is missing from the host context — the candidate would invent colours')
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const { proposeFigure } = getDeps()
|
|
261
|
+
const result = await proposeFigure({
|
|
262
|
+
prompt,
|
|
263
|
+
palette,
|
|
264
|
+
base,
|
|
265
|
+
type: typeof args?.type === 'string' ? args.type : undefined
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
/*
|
|
269
|
+
**`proposed: true` 가 카드를 만든다.** 이 표시가 없으면 후보는 도구 추적 안에 묻히고,
|
|
270
|
+
화면에는 아무것도 생기지 않는다 — 그러면 모델이 「후보를 만들었습니다」라고 쓴 것이
|
|
271
|
+
거짓말이 된다(사용자가 없는 단추를 찾는다).
|
|
272
|
+
|
|
273
|
+
`label`·`reason` 이 카드에 그려지는 두 줄이고, `args` 는 같은 제안을 두 번 세지 않기
|
|
274
|
+
위한 열쇠의 일부다 — 같은 요청을 두 번 내면 카드가 하나로 접힌다.
|
|
275
|
+
*/
|
|
276
|
+
return {
|
|
277
|
+
proposed: true,
|
|
278
|
+
label: base ? `${result.source.type} 고치기` : `${result.source.type} 만들기`,
|
|
279
|
+
reason:
|
|
280
|
+
`등급 ${result.score.grade} · 삼각형 ${result.cost.triangles} · ` +
|
|
281
|
+
`한 도면 ${result.cost.at.drawCalls}회 · ${result.attempts}번에 만들었다`,
|
|
282
|
+
args: { prompt, figureId: base ? figureIdFrom(args, ctx?.state?.host) : undefined },
|
|
283
|
+
source: result.source,
|
|
284
|
+
score: result.score,
|
|
285
|
+
cost: result.cost,
|
|
286
|
+
violations: result.violations,
|
|
287
|
+
attempts: result.attempts
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
] as ToolSpec[]
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* 등록 — 부팅 시 1회.
|
|
296
|
+
*
|
|
297
|
+
* 범주가 하나다. twin 은 관찰과 생성을 둘로 갈랐는데(운영 협의에 생성 도구가 들려 있으면 대화의
|
|
298
|
+
* 성격이 흐려진다), figure 의 대화면은 저작면 하나뿐이고 그 자리에서 읽기와 후보 요청이 함께 필요하다.
|
|
299
|
+
* 대화면이 늘면 그때 가른다.
|
|
300
|
+
*/
|
|
301
|
+
export function registerFigureToolCategories(): void {
|
|
302
|
+
const authoring: ToolCategory = {
|
|
303
|
+
name: 'figure-authoring',
|
|
304
|
+
description: '저작 중인 Figure 의 정본·비용·발행 판정 조회와 후보 제안(저장 아님).',
|
|
305
|
+
guidance: [
|
|
306
|
+
'- **도형을 만들거나 고쳐 달라는 요청에는 반드시 `proposeFigureCandidate` 를 호출한다.**',
|
|
307
|
+
' 호출 없이 "후보를 만들었습니다" · "적용 버튼을 누르세요" 라고 쓰는 것은 **거짓이다** — 화면에 후보 칸이 생기지 않으므로 사용자는 존재하지 않는 버튼을 찾는다.',
|
|
308
|
+
' 후보 화면(「지금 / 후보」 두 칸)은 **도구 호출 결과에서만** 만들어진다. 문장으로는 아무 일도 일어나지 않는다.',
|
|
309
|
+
'- 정본을 직접 저장하지 않는다. 후보만 내고, 받을지 버릴지는 저작자가 화면에서 정한다.',
|
|
310
|
+
'- **고치라는 요청이면 먼저 `getFigureSource` 를 부른다.** 읽지 않고 고치면 짐작을 상대로 후보를 내게 되고, 부품 이름이 갈려 이미 배치된 보드의 바인딩이 끊긴다.',
|
|
311
|
+
'- 타입 이름을 지어내지 않는다 — `listFigures` 로 확인한다. **타입 이름은 저장되는 식별자**이고 보드가 그 이름으로 도형을 찾는다.',
|
|
312
|
+
'- 발행이 안 되는 이유를 물으면 `inspectFigure` 를 부른다. 판정 없이 "발행할 수 있습니다"라고 말하지 않는다.',
|
|
313
|
+
' 판정의 각 항목은 **왜 문제인지와 무엇을 바꾸면 되는지**를 함께 담고 있다. 그대로 전한다 — 요약하면 저작자가 어느 부품의 어느 축을 고칠지 다시 찾아야 한다.',
|
|
314
|
+
'- 무게·삼각형 수를 물으면 `getFigureSource` 의 `cost` 를 쓴다. 눈대중으로 세지 않는다.'
|
|
315
|
+
].join('\n'),
|
|
316
|
+
specs: figureToolSpecs()
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
registerToolCategory(authoring)
|
|
320
|
+
}
|
|
@@ -58,8 +58,12 @@ export class FigurePatch {
|
|
|
58
58
|
@Field({ nullable: true, description: 'The new master-data field definition.' })
|
|
59
59
|
properties?: string
|
|
60
60
|
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
/*
|
|
62
|
+
`state` 는 여기 없다 — **상태는 저장이 정하지 않는다.**
|
|
63
|
+
|
|
64
|
+
발행은 `releaseFigure` 가 한다. 저장으로 상태를 바꿀 수 있게 두면 발행된 판을 덮어쓰면서 같은
|
|
65
|
+
판 번호로 판본을 또 뜨게 되고(유니크 충돌), 무엇이 약속된 판인지도 흐려진다. board 도 같다.
|
|
66
|
+
*/
|
|
63
67
|
|
|
64
68
|
@Field({ nullable: true, description: 'A base64 encoded thumbnail image.' })
|
|
65
69
|
thumbnail?: string
|
|
@@ -106,3 +110,46 @@ export class FigureSaveResult {
|
|
|
106
110
|
@Field(type => [FigureFinding], { description: 'Non-blocking findings. The save succeeded despite these.' })
|
|
107
111
|
violations: FigureFinding[]
|
|
108
112
|
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 발행 판정 한 줄.
|
|
116
|
+
*
|
|
117
|
+
* `FigureFinding` 과 칸이 겹치지만 **`blocking` 이 하나 더 있다.** 저장 쪽 발견은 정의상 막지 않는
|
|
118
|
+
* 것들이라 그 칸이 필요 없었다. 발행 판정은 막는 것과 알리는 것을 함께 내므로, 어느 쪽인지를 값이
|
|
119
|
+
* 스스로 말해야 한다 — 코드를 보고 화면이 다시 분류하면 규칙이 두 벌이 된다.
|
|
120
|
+
*/
|
|
121
|
+
@ObjectType({ description: 'One finding from the release gate.' })
|
|
122
|
+
export class FigureGateFinding {
|
|
123
|
+
@Field({ description: 'Stable code — figure-model validation codes, or sizing-splits / sizing-loose.' })
|
|
124
|
+
code: string
|
|
125
|
+
|
|
126
|
+
@Field({ description: 'Human-readable message.' })
|
|
127
|
+
message: string
|
|
128
|
+
|
|
129
|
+
@Field({ nullable: true, description: 'Why this is a problem — what a person sees when it happens.' })
|
|
130
|
+
why?: string
|
|
131
|
+
|
|
132
|
+
@Field({ nullable: true, description: 'What to change, naming the part and the value.' })
|
|
133
|
+
how?: string
|
|
134
|
+
|
|
135
|
+
@Field({ nullable: true, description: 'Where it points: part names that came apart, or a path in the source.' })
|
|
136
|
+
at?: string
|
|
137
|
+
|
|
138
|
+
@Field({ description: 'True when this finding blocks the release.' })
|
|
139
|
+
blocking: boolean
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* 발행 판정.
|
|
144
|
+
*
|
|
145
|
+
* 화면이 **발행을 누르기 전에** 물어보는 자리다. 눌러 보고 거절당하는 것으로 알게 되면, 무엇을
|
|
146
|
+
* 고쳐야 하는지 모른 채 단추만 두드리게 된다.
|
|
147
|
+
*/
|
|
148
|
+
@ObjectType({ description: 'Whether a figure may be released, and why not.' })
|
|
149
|
+
export class FigureInspection {
|
|
150
|
+
@Field({ description: 'True when at least one finding blocks the release.' })
|
|
151
|
+
blocked: boolean
|
|
152
|
+
|
|
153
|
+
@Field(type => [FigureGateFinding], { description: 'Findings, blocking and advisory together.' })
|
|
154
|
+
findings: FigureGateFinding[]
|
|
155
|
+
}
|